Open In App

DateTime.IsLeapYear() Method in C#

Last Updated : 15 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

This method returns an indication of whether the specified year is a leap year or not. Here, the year is always interpreted as a year in the Gregorian calendar.

Syntax: 

public static bool IsLeapYear (int year);

Return Value: This method return true if year is a leap year otherwise it returns false.

Exception: This method will give ArgumentOutOfRangeException if the year is less than 1 or greater than 9999.

Below programs illustrate the use of above-discussed method:

Example 1: 

C#




// C# code to demonstrate the
// IsLeapYear(Int32) Method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // Checking the leap year between 2000 to 2019
        for (int y = 2000; y <= 2019; y++)
        {
 
            // using method
            if (DateTime.IsLeapYear(y))
            {
                Console.WriteLine("{0} is a Leap Year.", y);
            }
 
            else
            {
                Console.WriteLine("{0} is not a Leap Year.", y);
            }
        }
    }
}


Output: 

2000 is a Leap Year.
2001 is not a Leap Year.
2002 is not a Leap Year.
2003 is not a Leap Year.
2004 is a Leap Year.
2005 is not a Leap Year.
2006 is not a Leap Year.
2007 is not a Leap Year.
2008 is a Leap Year.
2009 is not a Leap Year.
2010 is not a Leap Year.
2011 is not a Leap Year.
2012 is a Leap Year.
2013 is not a Leap Year.
2014 is not a Leap Year.
2015 is not a Leap Year.
2016 is a Leap Year.
2017 is not a Leap Year.
2018 is not a Leap Year.
2019 is not a Leap Year.

Example 2:

C#




// C# code to demonstrate the
// IsLeapYear(Int32) Method
using System;
 
class GFG {
 
    // Main Method
    public static void Main()
    {
 
        // using method
        if (DateTime.IsLeapYear(9999))
        {
            Console.WriteLine("9999 is a Leap Year.");
        }
 
        else
        {
            Console.WriteLine("9999 is not a Leap Year.");
        }
 
        // using method will give an error
        // as year's value is greater than
        // 9999
        if (DateTime.IsLeapYear(10000))
        {
            Console.WriteLine(" 10000 is a Leap Year.");
        }
 
        else {
            Console.WriteLine("10000 is not a Leap Year.");
        }
    }
}


Runtime Error:

Unhandled Exception: 
System.ArgumentOutOfRangeException: Year must be between 1 and 9999. 
Parameter name: year 
 

Reference:  

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads