Skip to content
Tutorials Center
My learning

Topics

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.

C#
var cal = new System.Globalization.ThaiBuddhistCalendar();
Console.WriteLine(cal.GetYear(new DateTime(2026, 1, 1)));   // 2569

Try it

C# The same day in several calendars

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.

C# Week numbers and adding months

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

C#
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 DateTime has 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, and ar-SA prints Hijri. This sandbox has only the invariant culture, so use calendar objects directly, as in the examples.
  • GetWeekOfYear does not return ISO weeks unless you choose FirstFourDayWeek with Monday; even then, week 53 and week 1 at year boundaries can differ from ISO. Use ISOWeek.GetWeekOfYear and ISOWeek.GetYear for ISO 8601.
  • Calendar ranges are limited. The range of some calendars is narrow (HijriCalendar starts in the year 622, UmAlQuraCalendar covers roughly 1900 to 2077); Min/MaxSupportedDateTime tell 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 GetEra and GetYear together, and note that era data can come from the operating system, which may be out of date for the newest era.
  • Month arithmetic. AddMonths follows the calendar's month lengths and clamps the day if needed, the same way DateTime.AddMonths does for Gregorian dates.
  • DateTime.Parse and 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 DateOnly and DateTime for storage (always Gregorian), and convert for display only.