C# Reference ยท Date, Time and Globalization
Calendar classes
Reference for System.Globalization.Calendar in C#: GregorianCalendar, JulianCalendar, HijriCalendar and others, converting dates between calendars and week numbers.
A calendar class knows how to split a point in time into year, month and day for one calendar system. DateTime itself always stores a Gregorian date, but the classes in System.Globalization can read the same moment as a Julian, Hijri, Thai Buddhist, Japanese or other date, and can build a DateTime from a date in those calendars.
The base class is Calendar. Its subclasses include GregorianCalendar, JulianCalendar, HijriCalendar, UmAlQuraCalendar, PersianCalendar, HebrewCalendar, ThaiBuddhistCalendar, JapaneseCalendar, KoreanCalendar and TaiwanCalendar. Each culture has a default calendar, available as CultureInfo.Calendar.
var cal = new System.Globalization.ThaiBuddhistCalendar(); Console.WriteLine(cal.GetYear(new DateTime(2026, 1, 1))); // 2569
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public abstract class Calendar : ICloneable
| Member | Description |
|---|---|
GetYear(DateTime), GetMonth(DateTime), GetDayOfMonth(DateTime) |
The parts of a date in this calendar. |
GetDayOfWeek(DateTime), GetDayOfYear(DateTime) |
Weekday and day number. |
ToDateTime(year, month, day, hour, minute, second, millisecond) |
Build a DateTime from a date in this calendar. |
AddDays, AddMonths, AddYears |
Calendar-aware arithmetic. |
GetDaysInMonth, GetDaysInYear, GetMonthsInYear, IsLeapYear, IsLeapMonth |
Calendar structure. |
GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) |
Week number under a rule. |
MinSupportedDateTime, MaxSupportedDateTime |
The range the calendar can represent. |
Eras, GetEra(DateTime) |
Era numbers (important for Japanese calendars). |
TwoDigitYearMax |
Cutoff used to expand two-digit years. |
Errors: ArgumentOutOfRangeException when a date is outside MinSupportedDateTime/MaxSupportedDateTime, or when the year, month or day does not exist in the calendar.
Usage notes
- Everything is stored as Gregorian. A
DateTimehas no calendar. The calendar object is only a lens for reading or building values. - Culture formatting uses
CultureInfo.Calendar. In normal .NET,new CultureInfo("th-TH")prints Thai Buddhist years, andar-SAprints Hijri. This sandbox has only the invariant culture, so use calendar objects directly, as in the examples. GetWeekOfYeardoes not return ISO weeks unless you chooseFirstFourDayWeekwith Monday; even then, week 53 and week 1 at year boundaries can differ from ISO. UseISOWeek.GetWeekOfYearandISOWeek.GetYearfor ISO 8601.- Calendar ranges are limited. The range of some calendars is narrow (
HijriCalendarstarts in the year 622,UmAlQuraCalendarcovers roughly 1900 to 2077);Min/MaxSupportedDateTimetell you where. - Leap rules differ. 2100 is not a Gregorian leap year but is a Julian one, and the Hebrew calendar has a leap month, so month counts per year vary.
- Eras matter for Japanese calendars. Use
GetEraandGetYeartogether, and note that era data can come from the operating system, which may be out of date for the newest era. - Month arithmetic.
AddMonthsfollows the calendar's month lengths and clamps the day if needed, the same wayDateTime.AddMonthsdoes for Gregorian dates. DateTime.Parseand calendars. Parsing text written in a non-Gregorian calendar needs a culture whose default or optional calendar matches; do not guess the year offsets by hand.- Prefer
DateOnlyandDateTimefor storage (always Gregorian), and convert for display only.