DateTime.IsLeapYear Method

July 28, 2014

Returns an indication whether the specified year is a leap year.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static bool IsLeapYear(
	int year
)

Parameters

year
Type: System.Int32
A 4-digit year.

Return Value

Type: System.Boolean
true if year is a leap year; otherwise, false.

ExceptionCondition
ArgumentOutOfRangeException

year is less than 1 or greater than 9999.

year is specified as a 4-digit base 10 number; for example, 1996.

year is always interpreted as a year in the Gregorian calendar. To determine whether a particular year was a leap year in some other calendar, call that calendar object's IsLeapYear method.

The following example uses the IsLeapYear method to determine which years between 1994 and 2014 are leap years. The example also illustrates the result when the AddYears method is used to add a year to a leap day.


using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      for (int year = 1994; year <= 2014; year++)
      {
         if (DateTime.IsLeapYear(year))
         {
            outputBlock.Text += String.Format("{0} is a leap year.", year) + "\n";
            DateTime leapDay = new DateTime(year, 2, 29);
            DateTime nextYear = leapDay.AddYears(1);
            outputBlock.Text += String.Format("   One year from {0} is {1}.",
                              leapDay.ToString("d"),
                              nextYear.ToString("d")) + "\n";
         }
      }
   }
}
// The example produces the following output:
//       1996 is a leap year.
//          One year from 2/29/1996 is 2/28/1997.
//       2000 is a leap year.
//          One year from 2/29/2000 is 2/28/2001.
//       2004 is a leap year.
//          One year from 2/29/2004 is 2/28/2005.
//       2008 is a leap year.
//          One year from 2/29/2008 is 2/28/2009.
//       2012 is a leap year.
//          One year from 2/29/2012 is 2/28/2013.


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft