DateTime Structure

Namespace: System
Assembly: mscorlib.dll

Represents an instant in time, typically expressed as a date and a time of day. The value is an ordered pair of date and time components that represents a specific point in time. The DateTime structure is a value type that represents dates ranging from 12:00:00 AM January 1, 0001 to 11:59:59 PM December 31, 9999, in the Gregorian calendar.

Summary

Properties

Date
Gets the date component of this instance.
Day
Gets the day of the month represented by this instance.
DayOfWeek
Gets the day of the week represented by this instance.
DayOfYear
Gets the day of the year represented by this instance.
Hour
Gets the hour component of the value of this instance.
Kind
Gets a value indicating whether the specified date and time is Coordinated Universal Time (UTC), is local time, or is neither.
Millisecond
Gets the milliseconds component of the value of this instance.
Minute
Gets the minute component of the value of this instance.
Month
Gets the month component of the value of this instance.
Now
Gets a value that is set to the current date and time on this computer, expressed as the local time.
Second
Gets the seconds component of the value of this instance.
TimeOfDay
Gets the time interval corresponding to the time-of-day component of this instance.
Today
Gets the current date and time on this computer, expressed as the local time, and truncates to a single day's 24 hours.
UtcNow
Gets a value that is set to the current Coordinated Universal Time (UTC) date and time on this computer.
Year
Gets the year component of the value of this instance.

Constructors

DateTime(Int32, Int32, Int32)
Initializes a new instance of the DateTime structure to the specified number of years, months, and days.
DateTime(Int32, Int32, Int32, Int32, Int32, Int32)
Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

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(Int32 months)
Adds the specified number of months to this instance.
AddSeconds(Double value)
Adds the specified number of seconds to this instance.
AddTicks(Int64 value)
Adds the specified number of ticks to this instance.
AddYears(Int32 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 indicating 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.
Subtract(DateTime value)
Subtracts the specified date and time value from this instance.
ToString()
Converts the value of this instance to its equivalent string representation.

Operators

==
Compares two DateTime instances for equality.
!=
Compares two DateTime instances for inequality.
>
Compares two DateTime instances to determine if the first is later than the second.
<
Compares two DateTime instances to determine if the first is earlier than the second.
>=
Compares two DateTime instances to determine if the first is later than or equal to the second.
<=
Compares two DateTime instances to determine if the first is earlier than or equal to the second.

Fields

MaxValue
The greatest possible DateTime value, which is equivalent to 12/31/9999 11:59:59 PM.
MinValue
The earliest possible DateTime value, which is equivalent to 1/1/0001 12:00:00 AM.

Detailed Members

Date Property

Gets the date component of this instance.

public DateTime Date { get; }

Day Property

Gets the day of the month represented by this instance.

public int Day { get; }

DayOfWeek Property

Gets the day of the week represented by this instance.

public DayOfWeek DayOfWeek { get; }

DayOfYear Property

Gets the day of the year represented by this instance.

public int DayOfYear { get; }

Hour Property

Gets the hour component of the value of this instance.

public int Hour { get; }

Kind Property

Gets a value indicating whether the specified date and time is Coordinated Universal Time (UTC), is local time, or is neither.

public DateTimeKind Kind { get; }

Millisecond Property

Gets the milliseconds component of the value of this instance.

public int Millisecond { get; }

Minute Property

Gets the minute component of the value of this instance.

public int Minute { get; }

Month Property

Gets the month component of the value of this instance.

public int Month { get; }

Now Property

Gets a value that is set to the current date and time on this computer, expressed as the local time.

public static DateTime Now { get; }

Second Property

Gets the seconds component of the value of this instance.

public int Second { get; }

TimeOfDay Property

Gets the time interval corresponding to the time-of-day component of this instance.

public TimeSpan TimeOfDay { get; }

Today Property

Gets the current date and time on this computer, expressed as the local time, and truncates to a single day's 24 hours.

public static DateTime Today { get; }

UtcNow Property

Gets a value that is set to the current Coordinated Universal Time (UTC) date and time on this computer.

public static DateTime UtcNow { get; }

Year Property

Gets the year component of the value of this instance.

public int Year { get; }

DateTime Constructor

Initializes a new instance of the DateTime structure to the specified number of years, months, and days.

public DateTime(int year, int month, int day)
Parameters:
  • year: The year (1 through 9999).
  • month: The month (1 through 12).
  • day: The day (1 through 31).

DateTime Constructor

Initializes a new instance of the DateTime structure to the specified year, month, day, hour, minute, and second.

public DateTime(int year, int month, int day, int hour, int minute, int second)
Parameters:
  • year: The year (1 through 9999).
  • month: The month (1 through 12).
  • day: The day (1 through 31).
  • hour: The hour (0 through 23).
  • minute: The minute (0 through 59).
  • second: The second (0 through 59).

AddDays Method

Adds the specified number of days to this instance.

public DateTime AddDays(double value)
Parameters:
  • value: The number of days to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of days added.

Example:

DateTime today = DateTime.Today;
DateTime tomorrow = today.AddDays(1);
Console.WriteLine($"Today: {today.ToShortDateString()}");
Console.WriteLine($"Tomorrow: {tomorrow.ToShortDateString()}");

AddHours Method

Adds the specified number of hours to this instance.

public DateTime AddHours(double value)
Parameters:
  • value: The number of hours to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of hours added.

AddMilliseconds Method

Adds the specified number of milliseconds to this instance.

public DateTime AddMilliseconds(double value)
Parameters:
  • value: The number of milliseconds to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of milliseconds added.

AddMinutes Method

Adds the specified number of minutes to this instance.

public DateTime AddMinutes(double value)
Parameters:
  • value: The number of minutes to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of minutes added.

AddMonths Method

Adds the specified number of months to this instance.

public DateTime AddMonths(int months)
Parameters:
  • months: The number of months to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of months added.

AddSeconds Method

Adds the specified number of seconds to this instance.

public DateTime AddSeconds(double value)
Parameters:
  • value: The number of seconds to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of seconds added.

AddTicks Method

Adds the specified number of ticks to this instance.

public DateTime AddTicks(long ticks)
Parameters:
  • ticks: The number of ticks to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of ticks added.

AddYears Method

Adds the specified number of years to this instance.

public DateTime AddYears(int value)
Parameters:
  • value: The number of years to add. The value can be positive or negative.
Returns:

A new DateTime instance with the specified number of years added.

CompareTo Method

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.

public int CompareTo(DateTime value)
Parameters:
  • value: The DateTime object to compare with the current instance.
Returns:

A signed integer that indicates the relative order of the objects being compared. Less than zero: current instance precedes value. Zero: current instance occurs in the same position as value. Greater than zero: current instance follows value.

Equals Method

Returns a value indicating whether this instance is equal to a specified DateTime value.

public bool Equals(DateTime value)
Parameters:
  • value: The DateTime object to compare with this instance.
Returns:

true if the value of the value parameter is equal to the value of this instance; otherwise, false.

Parse Method

Converts the string representation of a date and time to its DateTime equivalent.

public static DateTime Parse(string s)
Parameters:
  • s: A string that contains a date and time to convert.
Returns:

The DateTime value that is equivalent to the date and time contained in s.

Example:

string dateString = "2023-10-27 10:30:00";
DateTime parsedDate = DateTime.Parse(dateString);
Console.WriteLine($"Parsed Date: {parsedDate}");

Subtract Method

Subtracts the specified date and time value from this instance.

public TimeSpan Subtract(DateTime value)
Parameters:
  • value: The DateTime object to subtract from this instance.
Returns:

A TimeSpan object that represents the difference between the two dates.

ToString Method

Converts the value of this instance to its equivalent string representation.

public string ToString()
Returns:

A string that represents the value of this instance.

Example:

DateTime now = DateTime.Now;
Console.WriteLine($"Current Date and Time: {now.ToString()}");
Console.WriteLine($"Short Date: {now.ToShortDateString()}");
Console.WriteLine($"Short Time: {now.ToShortTimeString()}");

Operators

Equality Operator (==)

Compares two DateTime instances for equality.

public static bool operator ==(DateTime t1, DateTime t2)

Returns true if t1 and t2 represent the same point in time; otherwise, false.

Inequality Operator (!=)

Compares two DateTime instances for inequality.

public static bool operator !=(DateTime t1, DateTime t2)

Returns true if t1 and t2 do not represent the same point in time; otherwise, false.

Greater Than Operator (>)

Compares two DateTime instances to determine if the first is later than the second.

public static bool operator >(DateTime t1, DateTime t2)

Returns true if t1 is later than t2; otherwise, false.

Less Than Operator (<)

Compares two DateTime instances to determine if the first is earlier than the second.

public static bool operator <(DateTime t1, DateTime t2)

Returns true if t1 is earlier than t2; otherwise, false.

Greater Than or Equal To Operator (>=)

Compares two DateTime instances to determine if the first is later than or equal to the second.

public static bool operator >=(DateTime t1, DateTime t2)

Returns true if t1 is later than or equal to t2; otherwise, false.

Less Than or Equal To Operator (<=)

Compares two DateTime instances to determine if the first is earlier than or equal to the second.

public static bool operator <=(DateTime t1, DateTime t2)

Returns true if t1 is earlier than or equal to t2; otherwise, false.

Fields

MaxValue Field

The greatest possible DateTime value, which is equivalent to 12/31/9999 11:59:59 PM.

public static readonly DateTime MaxValue;

MinValue Field

The earliest possible DateTime value, which is equivalent to 1/1/0001 12:00:00 AM.

public static readonly DateTime MinValue;