DateTime.DayOfYear Propriedade
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Obtém o dia do ano representado por essa instância.
public:
property int DayOfYear { int get(); };
public int DayOfYear { get; }
member this.DayOfYear : int
Public ReadOnly Property DayOfYear As Integer
O dia do ano, expresso como um valor entre 1 e 366.
O exemplo a seguir exibe o dia do ano de 31 de dezembro para os anos de 2010-2020 no calendário gregoriano. Observe que o exemplo mostra que 31 de dezembro é o 366º dia do ano em anos bissextos.
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)
A DayOfYear propriedade leva em conta anos bissextos quando calcula o dia do ano. O valor da propriedade sempre reflete o dia do ano no calendário gregoriano, independentemente do calendário atual da cultura atual. Para recuperar o dia do ano em um calendário diferente, chame o Calendar.GetDayOfYear método desse calendário.
Produto | Versões |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Comentários do .NET
O .NET é um projeto código aberto. Selecione um link para fornecer comentários: