Namespace: System.Globalization
Represents the Gregorian calendar. The Gregorian calendar is the most widely used civil calendar in the world today.
The GregorianCalendar
class supports the standard Gregorian calendar used in most of the world. It handles date calculations, including leap years and the number of days in each month.
GregorianCalendar()
Initializes a new instance of the GregorianCalendar
class.
GregorianCalendar(GregorianCalendarTypes calendarType)
Initializes a new instance of the GregorianCalendar
class with the specified calendar type.
Eras
Gets an array of the culture-specific era names for the current calendar.
CalendarTypeName
Gets the name of the current calendar.
CalendarType
Gets the type of the current calendar.
IsFixedDateInTwoDigitYear
Gets a value that indicates whether the leap year is determined by a fixed date.
TwoDigitYearMax
Gets or sets the property that represents the last year of a 100-year period used to evaluate two-digit years.
CalendarMode
Gets the calendar mode for the GregorianCalendar
.
AddDays(DateTime time, int days)
Adds a specified number of days to the specified date and time.
AddHours(DateTime time, int hours)
Adds a specified number of hours to the specified date and time.
AddMilliseconds(DateTime time, double milliseconds)
Adds a specified number of milliseconds to the specified date and time.
AddMonths(DateTime time, int months)
Adds a specified number of months to the specified date and time.
AddSeconds(DateTime time, double seconds)
Adds a specified number of seconds to the specified date and time.
AddWeeks(DateTime time, int weeks)
Adds a specified number of weeks to the specified date and time.
AddYears(DateTime time, int years)
Adds a specified number of years to the specified date and time.
GetDaysInMonth(int year, int month, int era)
Returns the number of days in the specified month and year in the specified era.
GetDaysInYear(int year, int era)
Returns the number of days in the specified year in the specified era.
GetLeapMonth(int year, int era)
Returns the leap month in the specified year and era.
GetMonth(DateTime time)
Returns the month part of the specified date.
GetYear(DateTime time)
Returns the year part of the specified date.
IsSunday(int year, int month, int day, int era)
Determines whether the specified date is a Sunday.
IsLeapYear(int year, int era)
Determines whether the specified year in the specified era is a leap year.
ToString()
Returns a string that represents the current object.
The GregorianCalendar
class is derived from the abstract Calendar
class. It represents the calendar system introduced by Pope Gregory XIII in 1582. It is the most widely used civil calendar in the world.
This calendar has 12 months, with 31 days in January, March, May, July, August, October, and December; 30 days in April, June, September, and November; and 28 days in February in a common year and 29 days in a leap year.
A leap year occurs every 4 years, except for years divisible by 100 but not by 400. For example, 1900 was not a leap year, but 2000 was.
The GregorianCalendar
class also supports different GregorianCalendarTypes
which affect how certain dates are interpreted, such as the start of the week and how two-digit years are resolved.
using System;
using System.Globalization;
public class GregorianCalendarExample
{
public static void Main(string[] args)
{
// Create a GregorianCalendar instance
GregorianCalendar gc = new GregorianCalendar();
// Get the current date
DateTime today = DateTime.Today;
// Get components of the date
int year = gc.GetYear(today);
int month = gc.GetMonth(today);
int day = today.Day; // DateTime.Day property is usually sufficient
Console.WriteLine($"Today's date components:");
Console.WriteLine($"Year: {year}");
Console.WriteLine($"Month: {month}");
Console.WriteLine($"Day: {day}");
// Check if it's a leap year
if (gc.IsLeapYear(year))
{
Console.WriteLine($"{year} is a leap year.");
}
else
{
Console.WriteLine($"{year} is not a leap year.");
}
// Add days to a date
DateTime futureDate = gc.AddDays(today, 10);
Console.WriteLine($"10 days from now: {futureDate.ToShortDateString()}");
// Get days in a specific month
int daysInFebruary = gc.GetDaysInMonth(2024, 2); // 2024 is a leap year
Console.WriteLine($"Number of days in February 2024: {daysInFebruary}");
daysInFebruary = gc.GetDaysInMonth(2023, 2); // 2023 is not a leap year
Console.WriteLine($"Number of days in February 2023: {daysInFebruary}");
}
}