Reading and Display Date Values

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 an UmAlQura string and convert it to a DateTime value.

The appearance of date values is dependent on the culture, international standards and 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: