DateTime.IsLeapYear() Method in C#

This method returns an indication 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to demonstrate the
// IsLeapYear(Int32) Method
using System;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Checing 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);
            }
        }
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Runtime Error:

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

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.