System.Globalization.GregorianCalendar Class

Namespace: System.Globalization

Overview

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.

Constructors

Properties

Methods

Remarks

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.

Example Usage:

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}");
    }
}

Requirements