System.DateTime Struct
Fields
-
MinValue
static DateTime MinValue { get; }
Gets the smallest possible
DateTime
value, which is equivalent to 12:00:00 midnight January 1, 0001, where midnight is represented by the earliest time possible on that day. -
MaxValue
static DateTime MaxValue { get; }
Gets the largest possible
DateTime
value, which is equivalent to 11:59:59 P.M. on December 31, 9999, coordinated universal time.
Properties
-
Date
DateTime Date { get; }
Gets the date component of this instance.
-
Day
int Day { get; }
Gets the day of the week represented by this instance.
-
DayOfWeek
DayOfWeek DayOfWeek { get; }
Gets the day of the week represented by this instance.
-
DayOfYear
int DayOfYear { get; }
Gets the day of the year represented by this instance.
-
Hour
int Hour { get; }
Gets the hour component of the date and time represented by this instance.
-
Kind
DateTimeKind Kind { get; }
Gets a value that indicates whether the
DateTime
value is Coordinated Universal Time (UTC), specific to local time, or is neither UTC nor local time. -
Millisecond
int Millisecond { get; }
Gets the millisecond component of the date and time represented by this instance.
-
Minute
int Minute { get; }
Gets the minute component of the date and time represented by this instance.
-
Month
int Month { get; }
Gets the month component of the date and time represented by this instance.
-
Second
int Second { get; }
Gets the seconds component of the date and time represented by this instance.
-
Ticks
long Ticks { get; }
Gets the value of this instance expressed as the number of ticks.
-
TimeOfDay
TimeSpan TimeOfDay { get; }
Gets a
TimeSpan
object that represents the time of day for this instance. -
Year
int Year { get; }
Gets the year component of the date and time represented by this instance.
Methods
-
AddDays
DateTime AddDays(double days)
Returns a new
DateTime
instance that has the specified number of days added to the value of this instance.Example:DateTime dt = new DateTime(2023, 10, 26); DateTime newDt = dt.AddDays(5); // October 31, 2023 Console.WriteLine(newDt);
-
AddMonths
DateTime AddMonths(int months)
Returns a new
DateTime
instance that has the specified number of months added to the value of this instance.Example:DateTime dt = new DateTime(2023, 10, 26); DateTime newDt = dt.AddMonths(3); // January 26, 2024 Console.WriteLine(newDt);
-
AddYears
DateTime AddYears(int years)
Returns a new
DateTime
instance that has the specified number of years added to the value of this instance.Example:DateTime dt = new DateTime(2023, 10, 26); DateTime newDt = dt.AddYears(10); // October 26, 2033 Console.WriteLine(newDt);
-
Compare
static int Compare(DateTime t1, DateTime t2)
Compares two
DateTime
values and returns an integer that indicates their relative order.Example:DateTime dt1 = new DateTime(2023, 10, 26); DateTime dt2 = new DateTime(2023, 11, 1); int comparison = DateTime.Compare(dt1, dt2); // comparison will be -1 (dt1 is earlier) Console.WriteLine($"Comparison result: {comparison}");
-
DaysInMonth
static int DaysInMonth(int year, int month)
Returns the number of days in the specified month and year.
Example:int days = DateTime.DaysInMonth(2024, 2); // 2024 is a leap year, so this will be 29 Console.WriteLine($"Days in February 2024: {days}");
-
Equals
bool Equals(DateTime value)
Determines whether this instance and a specified
DateTime
value represent the same date and time. -
IsDaylightSavingTime
bool IsDaylightSavingTime()
Gets a value that indicates whether the current
DateTime
instance represents a daylight saving time. -
IsLeapYear
static bool IsLeapYear(int year)
Determines whether the specified year is a leap year.
Example:bool isLeap = DateTime.IsLeapYear(2024); // true Console.WriteLine($"Is 2024 a leap year? {isLeap}");
-
Parse
static DateTime Parse(string s)
Converts the string representation of a date and time to its
DateTime
equivalent.Example:string dateString = "10/26/2023 10:30:00 AM"; DateTime parsedDate = DateTime.Parse(dateString); Console.WriteLine($"Parsed date: {parsedDate}");
-
ParseExact
static DateTime 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. The conversion fails if the string or format isnull
or if the format is invalid.Example:string dateString = "26-Oct-2023"; DateTime parsedDate = DateTime.ParseExact(dateString, "dd-MMM-yyyy", System.Globalization.CultureInfo.InvariantCulture); Console.WriteLine($"Parsed date: {parsedDate}");
-
Subtract
DateTime Subtract(DateTime value)
Subtracts a specified instance of
DateTime
from this instance and returns aTimeSpan
indicating the difference.Example:DateTime dt1 = new DateTime(2023, 10, 26, 14, 30, 0); DateTime dt2 = new DateTime(2023, 10, 20, 10, 0, 0); TimeSpan difference = dt1.Subtract(dt2); Console.WriteLine($"Difference: {difference.Days} days, {difference.Hours} hours");
-
ToString
string ToString()
Converts the value of this instance to its equivalent string representation using the current culture.
-
ToString
string ToString(string format)
Converts the value of this instance to its equivalent string representation using the specified format.
Example:DateTime now = DateTime.Now; string formattedDate = now.ToString("yyyy-MM-dd HH:mm:ss"); Console.WriteLine($"Formatted date: {formattedDate}");
-
ToUniversalTime
DateTime ToUniversalTime()
Converts the value of this instance to Coordinated Universal Time (UTC).
-
Today
static DateTime Today { get; }
Gets the current date and time on this computer, still as the local time.
-
Now
static DateTime Now { get; }
Gets a
DateTime
object that is set to the current date and time on this computer, expressed as the local time.
Operators
-
+
static DateTime operator +(DateTime d, TimeSpan t)
Adds a specified time interval to a specified date and time value.
-
-
static DateTime operator -(DateTime d, TimeSpan t)
Subtracts a specified time interval from a specified date and time value.
-
==
static bool operator ==(DateTime t1, DateTime t2)
Indicates whether two instances of
DateTime
are equal. -
!=
static bool operator !=(DateTime t1, DateTime t2)
Indicates whether two instances of
DateTime
are not equal. -
<
static bool operator <(DateTime t1, DateTime t2)
Compares two
DateTime
values and returnstrue
if the first value is earlier than the second value; otherwise,false
. -
>
static bool operator >(DateTime t1, DateTime t2)
Compares two
DateTime
values and returnstrue
if the first value is later than the second value; otherwise,false
. -
<=
static bool operator <=(DateTime t1, DateTime t2)
Compares two
DateTime
values and returnstrue
if the first value is earlier than or equal to the second value; otherwise,false
. -
>=
static bool operator >=(DateTime t1, DateTime t2)
Compares two
DateTime
values and returnstrue
if the first value is later than or equal to the second value; otherwise,false
.