DateTime.DayOfYear DateTime.DayOfYear DateTime.DayOfYear DateTime.DayOfYear Property

定义

获取此实例所表示的日期是该年中的第几天。Gets the day of the year represented by this instance.

public:
 property int DayOfYear { int get(); };
public int DayOfYear { get; }
member this.DayOfYear : int
Public ReadOnly Property DayOfYear As Integer

属性值

该年中的第几天,表示为 1 和 366 之间的一个值。The day of the year, expressed as a value between 1 and 366.

示例

下面的示例显示公历中年2010-2020 年12月31日的日期。The following example displays the day of the year of December 31 for the years 2010-2020 in the Gregorian calendar. 请注意,该示例显示12月31日是一年中的第366日。Note that the example shows that December 31 is the 366th day of the year in leap years.

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)
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计算一年中的第几天时,属性将占用闰年。The DayOfYear property takes leap years into account when it calculates the day of the year. 无论当前线程区域性的当前日历如何,属性值始终反映公历中的第几天。The property value always reflects the day of the year in the Gregorian calendar, regardless of the current thread culture's current calendar. 若要检索不同日历中的一年中的第几天Calendar.GetDayOfYear ,请调用该日历的方法。To retrieve the day of the year in a different calendar, call the Calendar.GetDayOfYear method of that calendar.

适用于

另请参阅