Export (0) Print
Expand All

How to: Display Dates in Non-Gregorian Calendars

The DateTime and DateTimeOffset types use the Gregorian calendar as their default calendar. This means that calling a date and time value's ToString method displays the string representation of that date and time in the Gregorian calendar, even if that date and time was created using another calendar. This is illustrated in the following example, which uses two different ways to create a date and time value with the Persian calendar, but still displays those date and time values in the Gregorian calendar when it calls the ToString method. This example reflects two commonly used but incorrect techniques for displaying the date in a particular calendar.

������PersianCalendar persianCal = new PersianCalendar();

      DateTime persianDate = persianCal.ToDateTime(1387, 3, 18, 12, 0, 0, 0);
      Console.WriteLine(persianDate.ToString());

      persianDate = new DateTime(1387, 3, 18, persianCal);
      Console.WriteLine(persianDate.ToString());
      // The example displays the following output to the console: 
      //       6/7/2008 12:00:00 PM 
      //       6/7/2008 12:00:00 AM

Two different techniques can be used to display the date in a particular calendar. The first requires that the calendar be the default calendar for a particular culture. The second can be used with any calendar.

To display the date for a culture's default calendar

  1. Instantiate a calendar object derived from the Calendar class that represents the calendar to be used.

  2. Instantiate a CultureInfo object representing the culture whose formatting will be used to display the date.

  3. Call the Array.Exists<T> method to determine whether the calendar object is a member of the array returned by the CultureInfo.OptionalCalendars property. This indicates that the calendar can serve as the default calendar for the CultureInfo object. If it is not a member of the array, follow the instructions in the "To Display the Date in Any Calendar" section.

  4. Assign the calendar object to the Calendar property of the DateTimeFormatInfo object returned by the CultureInfo.DateTimeFormat property.

    NoteNote:

    The CultureInfo class also has a Calendar property. However, it is read-only and constant; it does not change to reflect the new default calendar assigned to the DateTimeFormatInfo.Calendar property.

  5. Call either the ToString or the ToString method, and pass it the CultureInfo object whose default calendar was modified in the previous step.

To display the date in any calendar

  1. Instantiate a calendar object derived from the Calendar class that represents the calendar to be used.

  2. Determine which date and time elements should appear in the string representation of the date and time value.

  3. For each date and time element that you want to display, call the calendar object's Get… method. The following methods are available:

    • GetYear, to display the year in the appropriate calendar.

    • GetMonth, to display the month in the appropriate calendar.

    • GetDayOfMonth, to display the number of the day of the month in the appropriate calendar.

    • GetHour, to display the hour of the day in the appropriate calendar.

    • GetMinute, to display the minutes in the hour in the appropriate calendar.

    • GetSecond, to display the seconds in the minute in the appropriate calendar.

    • GetMilliseconds , to display the milliseconds in the second in the appropriate calendar.

The example displays a date using two different calendars. It displays the date after defining the Hijri calendar as the default calendar for the ar-JO culture, and displays the date using the Persian calendar, which is not supported as an optional calendar by the fa-IR culture.

using System;
using System.Globalization;

public class CalendarDates
{
   public static void Main()
   {
      HijriCalendar hijriCal = new HijriCalendar();
      CalendarUtility hijriUtil = new CalendarUtility(hijriCal);
      DateTime hijriDate1 = new DateTime(1429, 6, 29, hijriCal);
      DateTimeOffset hijriDate2 = new DateTimeOffset(hijriDate1, 
                                  TimeZoneInfo.Local.GetUtcOffset(hijriDate1));
      CultureInfo jc = CultureInfo.CreateSpecificCulture("ar-JO");

      // Display the date using the Gregorian calendar.
      Console.WriteLine("Using the system default culture: {0}", 
                        hijriDate1.ToString("d"));
      // Display the date using the ar-JO culture's original default calendar.
      Console.WriteLine("Using the ar-JO culture's original default calendar: {0}", 
                        hijriDate1.ToString("d", jc));
      // Display the date using the Hijri calendar.
      Console.WriteLine("Using the ar-JO culture with Hijri as the default calendar:");
      // Display a Date value.
      Console.WriteLine(hijriUtil.DisplayDate(hijriDate1, jc));
      // Display a DateTimeOffset value.
      Console.WriteLine(hijriUtil.DisplayDate(hijriDate2, jc));

      Console.WriteLine();

      PersianCalendar persianCal = new PersianCalendar();
      CalendarUtility persianUtil = new CalendarUtility(persianCal);
      CultureInfo ic = CultureInfo.CreateSpecificCulture("fa-IR");

      // Display the date using the ir-FA culture's default calendar.
      Console.WriteLine("Using the ir-FA culture's default calendar: {0}",       
                        hijriDate1.ToString("d", ic));
      // Display a Date value.
      Console.WriteLine(persianUtil.DisplayDate(hijriDate1, ic));
      // Display a DateTimeOffset value.
      Console.WriteLine(persianUtil.DisplayDate(hijriDate2, ic));
   }
}

public class CalendarUtility
{
   private Calendar thisCalendar;
   private CultureInfo targetCulture;

   public CalendarUtility(Calendar cal)
   {
      this.thisCalendar = cal;
   }

   private bool CalendarExists(CultureInfo culture)
   {
      this.targetCulture = culture;
      return Array.Exists(this.targetCulture.OptionalCalendars, 
                          this.HasSameName);
   }

   private bool HasSameName(Calendar cal)
   {
      if (cal.ToString() == thisCalendar.ToString())
         return true;
      else 
         return false;
   }

   public string DisplayDate(DateTime dateToDisplay, CultureInfo culture)
   {
      DateTimeOffset displayOffsetDate = dateToDisplay;
      return DisplayDate(displayOffsetDate, culture);
   }

   public string DisplayDate(DateTimeOffset dateToDisplay, 
                             CultureInfo culture)
   {
      string specifier = "yyyy/MM/dd";

      if (this.CalendarExists(culture))
      {
         Console.WriteLine("Displaying date in supported {0} calendar...", 
                           this.thisCalendar.GetType().Name);
         culture.DateTimeFormat.Calendar = this.thisCalendar;
         return dateToDisplay.ToString(specifier, culture);
      }
      else
      {
         Console.WriteLine("Displaying date in unsupported {0} calendar...", 
                           thisCalendar.GetType().Name);

         string separator = targetCulture.DateTimeFormat.DateSeparator;

         return thisCalendar.GetYear(dateToDisplay.DateTime).ToString("0000") +
                separator +
                thisCalendar.GetMonth(dateToDisplay.DateTime).ToString("00") + 
                separator +
                thisCalendar.GetDayOfMonth(dateToDisplay.DateTime).ToString("00"); 
      }
   } 
}
// The example displays the following output to the console: 
//       Using the system default culture: 7/3/2008 
//       Using the ar-JO culture's original default calendar: 03/07/2008 
//       Using the ar-JO culture with Hijri as the default calendar: 
//       Displaying date in supported HijriCalendar calendar... 
//       1429/06/29 
//       Displaying date in supported HijriCalendar calendar... 
//       1429/06/29 
//        
//       Using the ir-FA culture's default calendar: 7/3/2008 
//       Displaying date in unsupported PersianCalendar calendar... 
//       1387/04/13 
//       Displaying date in unsupported PersianCalendar calendar... 
//       1387/04/13

Each CultureInfo object can support one or more calendars, which are indicated by the OptionalCalendars property. One of these is designated as the culture's default calendar and is returned by the read-only CultureInfo.Calendar property. Another of the optional calendars can be designated as the default by assigning a Calendar object that represents that calendar to the DateTimeFormatInfo.Calendar property returned by the CultureInfo.DateTimeFormat property. However, some calendars, such as the Persian calendar represented by the PersianCalendar class, do not serve as optional calendars for any culture.

The example defines a reusable calendar utility class, CalendarUtility, to handle many of the details of generating the string representation of a date using a particular calendar. The CalendarUtility class has the following members:

  • A parameterized constructor whose single parameter is a Calendar object in which a date is to be represented. This is assigned to a private field of the class.

  • CalendarExists, a private method that returns a Boolean value indicating whether the calendar represented by the CalendarUtility object is supported by the CultureInfo object that is passed to the method as a parameter. The method wraps a call to the Array.Exists<T> method, to which it passes the CultureInfo.OptionalCalendars array.

  • HasSameName, a private method assigned to the Predicate<T> delegate that is passed as a parameter to the Array.Exists<T> method. Each member of the array is passed to the method until the method returns true. The method determines whether the name of an optional calendar is the same as the calendar represented by the CalendarUtility object.

  • DisplayDate, an overloaded public method that is passed two parameters: either a DateTime or DateTimeOffset value to express in the calendar represented by the CalendarUtility object; and the culture whose formatting rules are to be used. Its behavior in returning the string representation of a date depends on whether the target calendar is supported by the culture whose formatting rules are to be used.

Regardless of the calendar used to create a DateTime or DateTimeOffset value in this example, that value is typically expressed as a Gregorian date. This is because the DateTime and DateTimeOffset types do not preserve any calendar information. Internally, they are represented as the number of ticks that have elapsed since midnight of January 1, 0001. The interpretation of that number depends on the calendar. For most cultures, the default calendar is the Gregorian calendar.

This example requires a reference to System.Core.dll.

Compile the code at the command line using csc.exe or vb.exe. To compile the code in Visual Studio, put it in a console application project template.

Community Additions

ADD
Show:
© 2014 Microsoft