DateTime.IsLeapYear() Method in C#

The DateTime.IsLeapYear() method in C# is used to determine whether a specified year is a leap year. This static method returns true if the year is a leap year, and false otherwise. A leap year occurs every 4 years, with exceptions for century years that are not divisible by 400.

Syntax

Following is the syntax −

public static bool IsLeapYear(int year);

Parameters

  • year − An integer representing the year to be checked. The year must be between 1 and 9999.

Return Value

Returns a bool value −

  • true if the specified year is a leap year

  • false if the specified year is not a leap year

How It Works

A leap year follows these rules −

  • Divisible by 4 AND not divisible by 100, OR

  • Divisible by 400

Leap Year Decision Tree Divisible by 4? No Yes Not Leap Year Divisible by 100? Yes No Leap Year Leap Year

Using IsLeapYear() with Regular Years

Example

using System;

public class Demo {
    public static void Main() {
        int year = 2019;
        Console.WriteLine("Year = " + year);
        if (DateTime.IsLeapYear(year)) {
            Console.WriteLine("Leap Year!");
        } else {
            Console.WriteLine("Not a Leap Year!");
        }
        
        // Test multiple years
        int[] testYears = {2020, 2021, 2024};
        foreach(int testYear in testYears) {
            Console.WriteLine($"{testYear}: {(DateTime.IsLeapYear(testYear) ? "Leap" : "Not Leap")} Year");
        }
    }
}

The output of the above code is −

Year = 2019
Not a Leap Year!
2020: Leap Year
2021: Not Leap Year
2024: Leap Year

Using IsLeapYear() with Century Years

Example

using System;

public class Demo {
    public static void Main() {
        // Century years - special leap year rules
        int[] centuryYears = {1900, 2000, 2100, 2400};
        
        foreach(int year in centuryYears) {
            bool isLeap = DateTime.IsLeapYear(year);
            Console.WriteLine($"{year}: {(isLeap ? "Leap" : "Not Leap")} Year");
        }
    }
}

The output of the above code is −

1900: Not Leap Year
2000: Leap Year
2100: Not Leap Year
2400: Leap Year

Handling Out of Range Years

The method throws an ArgumentOutOfRangeException if the year is outside the valid range (1 to 9999) −

Example

using System;

public class Demo {
    public static void Main() {
        try {
            int year = 101910;
            Console.WriteLine("Year = " + year);
            bool result = DateTime.IsLeapYear(year);
            Console.WriteLine(result ? "Leap Year!" : "Not a Leap Year!");
        }
        catch (ArgumentOutOfRangeException ex) {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}

The output of the above code is −

Year = 101910
Error: Year must be between 1 and 9999.
Parameter name: year

Conclusion

The DateTime.IsLeapYear() method provides a simple way to determine if a year is a leap year, handling all the complex rules automatically. It accepts years between 1 and 9999 and throws an exception for invalid ranges, making it reliable for date calculations and validations.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements