C# DateTime DayOfYear

Description

DateTime DayOfYear gets the day of the year represented by this instance.

Syntax

DateTime.DayOfYear has the following syntax.


public int DayOfYear { get; }

Example

The following example displays the day of the year of December 31 for the years 2010-2020 in the Gregorian calendar.


//from  w  ww  .  j  a  va  2  s .c  o  m
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)" : ""); 
      }  

   }
}

Note that the example shows that December 31 is the 366th day of the year in leap years.

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version