DateTimeFormatInfo.Calendar Property
Gets or sets the calendar to use for the current culture.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Globalization.CalendarThe calendar to use for the current culture. The default for InvariantInfo is a GregorianCalendar object.
| Exception | Condition |
|---|---|
| ArgumentNullException | The property is being set to null. |
| ArgumentOutOfRangeException | The property is being set to a Calendar object that is not valid for the current culture. |
| InvalidOperationException | The property is being set and the DateTimeFormatInfo object is read-only. |
The Calendar property accepts only calendars that are valid for the culture that is associated with the DateTimeFormatInfo object. The CultureInfo.OptionalCalendars property specifies the calendars that can be used by a particular culture, and the CultureInfo.Calendar property specifies the default calendar for the culture.
Changing the value of this property affects the following properties as well: MonthNames, AbbreviatedMonthNames, DayNames, AbbreviatedDayNames, CalendarWeekRule, FirstDayOfWeek, FullDateTimePattern, LongDatePattern, ShortDatePattern, YearMonthPattern, and MonthDayPattern.
For example, if the culture of the current thread is Japanese, this property accepts JapaneseCalendar, LocalizedGregorianCalendar, or USEnglishGregorianCalendar. When the JapaneseCalendar is used, the default long date specifier is "gg y'\x5e74'M'\x6708'd'\x65e5'". When the LocalizedGregorianCalendar, is used, the default long date specifier is "yyyy'\x5e74'M'\x6708'd'\x65e5'".
The following example defines a ChangeCalendar method that changes a culture's current calendar to a specified calendar unless it is already the current calendar or if it is not supported by the culture. The code that calls the method instantiates a CultureInfo object that represents the Arabic (Egypt) culture and first attempts to change its calendar to the Japanese calendar. Because the Japanese calendar is not supported, the method makes not change the culture's calendar. However, because the Umm al-Qura calendar is a member of the CultureInfo.OptionalCalendars collection, the method does succeed in making it the current calendar for the ar-EG culture.
Imports System.Globalization Module Example Public Sub Main() Dim ci As CultureInfo = CultureInfo.CreateSpecificCulture("ar-EG") Console.WriteLine("The current calendar for the {0} culture is {1}", ci.Name, CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar)) CalendarUtilities.ChangeCalendar(ci, New JapaneseCalendar()) Console.WriteLine("The current calendar for the {0} culture is {1}", ci.Name, CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar)) CalendarUtilities.ChangeCalendar(ci, New UmAlQuraCalendar()) Console.WriteLine("The current calendar for the {0} culture is {1}", ci.Name, CalendarUtilities.ShowCalendarName(ci.DateTimeFormat.Calendar)) End Sub End Module Public Class CalendarUtilities Private newCal As Calendar Private isGregorian As Boolean Public Shared Sub ChangeCalendar(ci As CultureInfo, cal As Calendar) Dim util As New CalendarUtilities(cal) ' Is the new calendar already the current calendar? If util.CalendarExists(ci.DateTimeFormat.Calendar) Then Exit Sub End If ' Is the new calendar supported? If Array.Exists(ci.OptionalCalendars, AddressOf util.CalendarExists) Then ci.DateTimeFormat.Calendar = cal End If End Sub Private Sub New(cal As Calendar) newCal = cal ' Is the new calendar a Gregorian calendar? isGregorian = cal.GetType().Name.Contains("Gregorian") End Sub Private Function CalendarExists(cal As Calendar) As Boolean If cal.ToString() = newCal.ToString Then If isGregorian Then If CType(cal, GregorianCalendar).CalendarType = CType(newCal, GregorianCalendar).CalendarType Then Return True End If Else Return True End If End If Return False End Function Public Shared Function ShowCalendarName(cal As Calendar) As String Dim calName As String = cal.ToString().Replace("System.Globalization.", "") If TypeOf cal Is GregorianCalendar Then calName += ", Type " + CType(cal, GregorianCalendar).CalendarType.ToString() End If Return calName End Function End Class ' The example displays the following output: ' The current calendar for the ar-EG culture is GregorianCalendar, Type Localized ' The current calendar for the ar-EG culture is GregorianCalendar, Type Localized ' The current calendar for the ar-EG culture is UmAlQuraCalendar
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1