C# DateTime Year

Description

DateTime Year gets the year component of the date represented by this instance.

Syntax

DateTime.Year has the following syntax.


public int Year { get; }

Example

The Year property returns the year of the current instance in the Gregorian calendar.

To retrieve the year using a particular calendar, you can call that calendar's GetYear method, as the following code shows.


/*from   ww  w.j  a va  2  s  .  c  o  m*/
using System;
using System.Globalization;
using System.Threading;

public class YearMethodExample
{
    public static void Main()
    {
      // Initialize date variable and display year
      DateTime date1 = new DateTime(2008, 1, 1, 6, 32, 0);
      Console.WriteLine(date1.Year);                    // Displays 2008 

      // Set culture to th-TH
      Thread.CurrentThread.CurrentCulture = new CultureInfo("th-TH");
      Console.WriteLine(date1.Year);                    // Displays 2008 

      // display year using current culture's calendar 
      Calendar thaiCalendar = CultureInfo.CurrentCulture.Calendar;
      Console.WriteLine(thaiCalendar.GetYear(date1));   // Displays 2551 

      // display year using Persian calendar
      PersianCalendar persianCalendar = new PersianCalendar();
      Console.WriteLine(persianCalendar.GetYear(date1)); // Displays 1386 
   }
}

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