MSDN Documentation

System.DateTime Struct

Represents an instant in time, typically expressed as a date and time of day. The value is expressed in Coordinated Universal Time (UTC) or local time. The DateTime structure is the .NET Framework's primary representation of a date and time.

Namespace

System

Assembly

mscorlib.dll, System.Runtime.dll

Inheritance

System.ObjectSystem.ValueTypeSystem.DateTime

Remarks

The DateTime structure is a value type that represents dates and times. It can be used to:

The DateTime structure stores date and time information as the number of ticks since midnight January 1, 0001, in the Gregorian calendar. A tick is one hundred nanoseconds or one ten-millionth of a second.

Members

The DateTime structure provides a rich set of properties and methods for working with dates and times. Here are some of the most commonly used members:

Properties

  • Date: Gets the date component of this instance.
  • Day: Gets the day of the month.
  • DayOfWeek: Gets the day of the week.
  • DayOfYear: Gets the day of the year.
  • Hour: Gets the hour component of this instance.
  • Kind: Gets a value that indicates whether the DateTime value is represented as local time, UTC, or with an offset from UTC.
  • Millisecond: Gets the millisecond component of this instance.
  • Minute: Gets the minute component of this instance.
  • Month: Gets the month component of this instance.
  • Second: Gets the second component of this instance.
  • Ticks: Gets the value of this instance expressed as the number of ticks.
  • TimeOfDay: Gets the time of day component of this instance.
  • Year: Gets the year component of this instance.

Constructors

  • DateTime(int year, int month, int day)
  • DateTime(int year, int month, int day, int hour, int minute, int second)
  • DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond)
  • DateTime(long ticks)
  • DateTime(long ticks, DateTimeKind kind)

Methods

  • AddDays(double value): Adds the specified number of days to this instance.
  • AddHours(double value): Adds the specified number of hours to this instance.
  • AddMilliseconds(double value): Adds the specified number of milliseconds to this instance.
  • AddMinutes(double value): Adds the specified number of minutes to this instance.
  • AddMonths(int months): Adds the specified number of months to this instance.
  • AddSeconds(double value): Adds the specified number of seconds to this instance.
  • AddTicks(long value): Adds the specified number of ticks to this instance.
  • AddYears(int value): Adds the specified number of years to this instance.
  • CompareTo(DateTime value): Compares the current instance to another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
  • Equals(DateTime value): Returns a value that indicates whether this instance is equal to a specified DateTime value.
  • Parse(string s): Converts the string representation of a date and time to its DateTime equivalent.
  • ParseExact(string s, string format, IFormatProvider provider): Converts the string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information.
  • ToString(): Converts the value of this instance to its equivalent string representation.
  • ToString(string format): Converts the value of this instance to its equivalent string representation using the specified format.

Static Properties

  • MaxValue: Gets the largest possible DateTime value.
  • MinValue: Gets the smallest possible DateTime value.
  • Now: Gets the current date and time on this computer, expressed as the local time.
  • UtcNow: Gets the current date and time on this computer, expressed as Coordinated Universal Time (UTC).

Example Usage

Here's a simple example demonstrating how to create and manipulate DateTime objects:


// Get the current date and time
DateTime now = DateTime.Now;
Console.WriteLine($"Current Date and Time: {now}"); // Output: Current Date and Time: 10/26/2023 10:30:15 AM (example)

// Create a specific date and time
DateTime christmas = new DateTime(2023, 12, 25, 9, 0, 0);
Console.WriteLine($"Christmas Day: {christmas.ToShortDateString()} at {christmas.ToShortTimeString()}"); // Output: Christmas Day: 12/25/2023 at 9:00 AM

// Add days to a date
DateTime nextWeek = now.AddDays(7);
Console.WriteLine($"Date next week: {nextWeek.ToLongDateString()}"); // Output: Date next week: Thursday, November 2, 2023 (example)

// Get the difference between two dates
TimeSpan difference = christmas - now;
Console.WriteLine($"Days until Christmas: {difference.TotalDays:F0}"); // Output: Days until Christmas: 60 (example)

// Format a DateTime object
string formattedDate = now.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine($"Formatted Date: {formattedDate}"); // Output: Formatted Date: 2023-10-26 10:30:15 (example)
            

See Also