DateTime.IsLeapYear() Method in C#
Last Updated :
15 Jul, 2021
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#
using System;
class GFG {
public static void Main()
{
for ( int y = 2000; y <= 2019; y++)
{
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#
using System;
class GFG {
public static void Main()
{
if (DateTime.IsLeapYear(9999))
{
Console.WriteLine( "9999 is a Leap Year." );
}
else
{
Console.WriteLine( "9999 is not a Leap Year." );
}
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
Please Login to comment...