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
4 位數的年份。
傳回
當 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
指定為4位數的10位數基底10位數;例如,1996。
year
一律會被視為西曆中的年份。 若要判斷特定年份在某個其他行事曆中是否為閏年,請呼叫該行事曆物件的 IsLeapYear
方法。