.NET Framework Class Library
DateTime..::.IsLeapYear Method

Updated: September 2009

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

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

Visual Basic (Declaration)
Public Shared Function IsLeapYear ( _
    year As Integer _
) As Boolean
Visual Basic (Usage)
Dim year As Integer
Dim returnValue As Boolean

returnValue = DateTime.IsLeapYear(year)
C#
public static bool IsLeapYear(
    int year
)
Visual C++
public:
static bool IsLeapYear(
    int year
)
JScript
public static function IsLeapYear(
    year : int
) : boolean

Parameters

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

Return Value

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

ExceptionCondition
ArgumentOutOfRangeException

year is less than 1 or greater than 9999.

Remarks

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.

Examples

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.

Visual Basic
Module IsLeapYear
   Public Sub Main()
      For year As Integer = 1994 to 2014
         If DateTime.IsLeapYear(year) Then
            Console.WriteLine("{0} is a leap year.", year)
            Dim leapDay As New Date(year, 2, 29)
            Dim nextYear As Date = leapDay.AddYears(1)
            Console.WriteLine("   One year from {0} is {1}.", _
                              leapDay.ToString("d"), _
                              nextYear.ToString("d"))
         End If
      Next
   End Sub
End Module
' The example displays 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.
C#
using System;

public class IsLeapYear
{
   public static void Main()
   {
      for (int year = 1994; year <= 2014; year++)
      {
         if (DateTime.IsLeapYear(year))
         {
            Console.WriteLine("{0} is a leap year.", year);
            DateTime leapDay = new DateTime(year, 2, 29);
            DateTime nextYear = leapDay.AddYears(1);
            Console.WriteLine("   One year from {0} is {1}.", 
                              leapDay.ToString("d"), 
                              nextYear.ToString("d"));
         }         
      }
   }
}
// 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.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Change History

Date

History

Reason

September 2009

Expanded the Remarks section.

Customer feedback.

July 2008

Revised the example.

Customer feedback.

Tags :


Community Content

Timothy Tannyer
IsLeapYear method - using PowerShell
Here is a sample of the isleapyear static method - using PowerShell:

$d1 = [system.datetime]  "Jan 1 2000"
$d2 = [System.datetime] "Jan 1 2002"
$d3 = [System.datetime] "Jan 1 2004"
$d4 = get-date

"{0} is a leap year: {1}" -f $d1.year,([system.datetime]::isleapyear($d1.year))
"{0} is a leap year: {1}" -f $d2.year,([system.datetime]::isleapyear($d2.year))
"{0} is a leap year: {1}" -f $d3.year,([system.datetime]::isleapyear($d3.year))
"{0} is a leap year: {1}" -f $d4.year,([system.datetime]::isleapyear($d4.year))


Page view tracker