DateTime.DayOfYear 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得由這個執行個體表示的一年天數。
public:
property int DayOfYear { int get(); };
public int DayOfYear { get; }
member this.DayOfYear : int
Public ReadOnly Property DayOfYear As Integer
屬性值
一年中的日期,以 1 到 366 之間的值表示。
範例
下列範例會顯示西曆中 2010 年 2010-2020 年 12 月 31 日的日期。 請注意,此範例顯示 12 月 31 日是閏年的第 366 天。
using System;
public class Example
{
public static void Main()
{
DateTime dec31 = new DateTime(2010, 12, 31);
for (int ctr = 0; ctr <= 10; ctr++) {
DateTime dateToDisplay = dec31.AddYears(ctr);
Console.WriteLine("{0:d}: day {1} of {2} {3}", dateToDisplay,
dateToDisplay.DayOfYear,
dateToDisplay.Year,
DateTime.IsLeapYear(dateToDisplay.Year) ?
"(Leap Year)" : "");
}
}
}
// The example displays the following output:
// 12/31/2010: day 365 of 2010
// 12/31/2011: day 365 of 2011
// 12/31/2012: day 366 of 2012 (Leap Year)
// 12/31/2013: day 365 of 2013
// 12/31/2014: day 365 of 2014
// 12/31/2015: day 365 of 2015
// 12/31/2016: day 366 of 2016 (Leap Year)
// 12/31/2017: day 365 of 2017
// 12/31/2018: day 365 of 2018
// 12/31/2019: day 365 of 2019
// 12/31/2020: day 366 of 2020 (Leap Year)
open System
let dec31 = DateTime(2010, 12, 31)
for i = 0 to 10 do
let dateToDisplay = dec31.AddYears i
let leap = if DateTime.IsLeapYear dateToDisplay.Year then "(Leap Year)" else ""
printfn $"{dateToDisplay:d}: day {dateToDisplay.DayOfYear} of {dateToDisplay.Year} {leap}"
// The example displays the following output:
// 12/31/2010: day 365 of 2010
// 12/31/2011: day 365 of 2011
// 12/31/2012: day 366 of 2012 (Leap Year)
// 12/31/2013: day 365 of 2013
// 12/31/2014: day 365 of 2014
// 12/31/2015: day 365 of 2015
// 12/31/2016: day 366 of 2016 (Leap Year)
// 12/31/2017: day 365 of 2017
// 12/31/2018: day 365 of 2018
// 12/31/2019: day 365 of 2019
// 12/31/2020: day 366 of 2020 (Leap Year)
Option Strict On
Module Example
Public Sub Main()
Dim dec31 As Date = #12/31/2010#
For ctr As Integer = 0 To 10
Dim dateToDisplay As Date = dec31.AddYears(ctr)
Console.WriteLine("{0:d}: day {1} of {2} {3}", dateToDisplay,
dateToDisplay.DayOfYear,
dateToDisplay.Year,
If(DateTime.IsLeapYear(dateToDisplay.Year),
"(Leap Year)", ""))
Next
End Sub
End Module
' The example displays the following output:
' 12/31/2010: day 365 of 2010
' 12/31/2011: day 365 of 2011
' 12/31/2012: day 366 of 2012 (Leap Year)
' 12/31/2013: day 365 of 2013
' 12/31/2014: day 365 of 2014
' 12/31/2015: day 365 of 2015
' 12/31/2016: day 366 of 2016 (Leap Year)
' 12/31/2017: day 365 of 2017
' 12/31/2018: day 365 of 2018
' 12/31/2019: day 365 of 2019
' 12/31/2020: day 366 of 2020 (Leap Year)
備註
當屬性計算年份的日期時,屬性 DayOfYear 會將閏年納入考慮。 不論目前文化特性的目前行事曆為何,屬性值一律會反映西曆中的年份日期。 若要在不同的日曆中擷取年份的日期,請呼叫 Calendar.GetDayOfYear 該行事曆的 方法。