One of the hottest topics in globalization is the topic of dates. Today we’ll see how we can use the DateTime class in the .NET framework to read and display different date values from different calendars.

The actual DateTime value is independent of the way it appears when you display it in your UI. Internally, all DateTime values are denoted as the total number of ticks that have elapsed since 12:00:00 midnight, January 1, 0001. The appearance of a DateTime value is the result of a formatting operation.

Reading Dates

First, you can initialize through the constructors, or by parsing the string representation of a date and time value. The Parse, ParseExact, TryParse, and TryParseExact methods all convert a string to its equivalent date and time value.

You can use the TryParse and TryParseExact methods to check if a particular string contains a valid representation of a DateTime value in addition to performing the conversion.

The following example uses the Parse method to parse a UmAlQura string and convert it to a DateTime value.

private static String ReadHijriDate(String InputStr)

{

         String OutStr= "";

         DateTime dt;

         System.Globalization.DateTimeFormatInfo HijriDTFI;

         try

         {

         HijriDTFI = new System.Globalization.CultureInfo("ar-SA", false).DateTimeFormat;

         HijriDTFI.Calendar = new System.Globalization.UmAlQuraCalendar();

         dt = DateTime.Parse(InputStr, HijriDTFI);

         OutStr = dt.Date.ToString("dddd, dd MMMM, yyyy");

         }

         catch (Exception ex)

         {

         MessageBox.Show(ex.Message);

         OutStr = "Error";

         }

         return OutStr;

}

 

Displaying Dates

The appearance of date values is dependent on the culture, international standards, application requirements. The DateTime offers a great deal of flexibility in formatting date and time values through the overloads of its ToString method.

 The default DateTime.ToString() method returns the string representation of a date and time value using the current culture's short date and long time pattern. However, you can display the dates in the different calendars that you need. For example, the below code displays the dateTime in a UmAlQura date, notice here that the range for the UmAlQura calendar is much less than the Gregorian calendar, therefore you need to fallback to the HijriCalendar. Check out this code:

private static String DisplayHijriDate(DateTime dt)
{
         String OutStr = "";
         System.Globalization.DateTimeFormatInfo HijriDTFI;
         try
  {
         //dt = Convert.ToDateTime(InputStr);
         HijriDTFI = new System.Globalization.CultureInfo("ar-SA", false).DateTimeFormat;
         HijriDTFI.Calendar = new System.Globalization.UmAlQuraCalendar();
         if (dt > HijriDTFI.Calendar.MaxSupportedDateTime)
         {
                 //We fell back to Hijri calendar, when we reached UmAlQura Max limit
                 HijriDTFI.Calendar = new System.Globalization.HijriCalendar();
         }
         OutStr = dt.Date.ToString("dddd, dd MMMM, yyyy", HijriDTFI);
         }
         catch (Exception ex)
         {
                 MessageBox.Show(ex.Message);
                 OutStr = "Error";
         }
         return OutStr;
}

 

I hope you found this blog useful and demonstrated how to read and display date through the DateTime class.