DateTime.IsLeapYear(Int32) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定的年份是否为闰年的指示。
public:
static bool IsLeapYear(int year);
public static bool IsLeapYear (int year);
static member IsLeapYear : int -> bool
Public Shared Function IsLeapYear (year As Integer) As Boolean
参数
- year
- Int32
四位数年份。
返回
如果 true
是闰年,则为 year
;否则为 false
。
例外
year
小于 1 或大于 9999。
示例
下面的示例使用 IsLeapYear 方法来确定1994和2014之间的年份是闰年。 该示例还说明了 AddYears 使用方法将年份添加到闰日时的结果。
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.
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.
注解
year
指定为四位数基数为10的数字;例如,1996。
year
在公历中始终解释为年份。 若要确定某一年份是否为其他日历中的闰年,请调用该日历对象的 IsLeapYear
方法。