DateTime
Struct · namespace System · Since 0.19
struct DateTime : IComparable<DateTime>, IEquatable<DateTime>, IHashableA date and time of day, local or UTC.
var now = DateTime.Now();Console.WriteLine(now.ToString("yyyy-MM-dd HH:mm"));var deadline = now.AddDays(3);TimeSpan left = deadline.Subtract(now); // 3.00:00:00A DateTime counts ticks of 100 ns from 0001-01-01 to 9999-12-31 (the Gregorian calendar), either in local time
or in UTC (DateTime.IsUtc); DateTime.Now is local, DateTime.UtcNow UTC. The clock and the time zone come from
the operating system. Like the .NET type, with methods instead of properties.
Fields
Section titled “Fields”| Name | Description |
|---|---|
Ticks |
The ticks of 100 ns since 0001-01-01 00:00. |
IsUtc |
Whether the time is UTC (otherwise it is local time). |
int64 TicksThe ticks of 100 ns since 0001-01-01 00:00.
bool IsUtcWhether the time is UTC (otherwise it is local time).
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
UtcNow |
The current date and time in UTC. |
Now |
The current date and time in local time. |
Today |
today’s date (local) at 00:00 |
Create |
A local date at 00:00. |
CreateUtc |
A date and time in UTC. |
FromUnixSeconds |
The UTC date and time seconds seconds after 1970-01-01 00:00 UTC (Unix time). |
FromUnixMilliseconds |
The UTC date and time milliseconds milliseconds after 1970-01-01 00:00 UTC. |
IsLeapYear |
Whether year is a leap year in the Gregorian calendar. |
DaysInMonth |
The number of days of month (1 to 12) in year. |
Parse |
Reads a date and time: yyyy-MM-dd, optionally followed by T or a space and HH:mm[:ss[.fffffff]], then Z (UTC) or +hh:mm / -hh:mm (UTC, converted from that offset). |
UtcNow
Section titled “UtcNow”static DateTime UtcNow()The current date and time in UTC.
static DateTime Now()The current date and time in local time.
static DateTime Today()today’s date (local) at 00:00
Create
Section titled “Create”static DateTime Create(int year, int month, int day)A local date at 00:00.
Panics when the date does not exist.
static DateTime Create(int year, int month, int day, int hour, int minute, int second)A local date and time.
Panics when the date or time does not exist.
static DateTime Create(int year, int month, int day, int hour, int minute, int second, int millisecond)A local date and time with milliseconds.
Panics when the date or time does not exist.
CreateUtc
Section titled “CreateUtc”static DateTime CreateUtc(int year, int month, int day, int hour, int minute, int second)A date and time in UTC.
Panics when the date or time does not exist.
FromUnixSeconds
Section titled “FromUnixSeconds”static DateTime FromUnixSeconds(int64 seconds)The UTC date and time seconds seconds after 1970-01-01 00:00 UTC (Unix time).
FromUnixMilliseconds
Section titled “FromUnixMilliseconds”static DateTime FromUnixMilliseconds(int64 milliseconds)The UTC date and time milliseconds milliseconds after 1970-01-01 00:00 UTC.
IsLeapYear
Section titled “IsLeapYear”static bool IsLeapYear(int year)Whether year is a leap year in the Gregorian calendar.
DaysInMonth
Section titled “DaysInMonth”static int DaysInMonth(int year, int month)The number of days of month (1 to 12) in year.
static ParseError<DateTime> Parse(string text)Reads a date and time: yyyy-MM-dd, optionally followed by T or a space and HH:mm[:ss[.fffffff]], then Z
(UTC) or +hh:mm / -hh:mm (UTC, converted from that offset). Without a zone the time is local.
Errors
ParseError.Invalid: the text has another format.ParseError.OutOfRange: the date or time does not exist.
Methods
Section titled “Methods”| Name | Description |
|---|---|
ToUnixSeconds |
The seconds since 1970-01-01 00:00 UTC (Unix time); a local time is converted to UTC first. |
ToUnixMilliseconds |
The milliseconds since 1970-01-01 00:00 UTC; a local time is converted to UTC first. |
Year |
The year (1 to 9999). |
Month |
The month (1 to 12). |
Day |
The day of the month (1 to 31). |
Hour |
The hour (0 to 23). |
Minute |
The minute (0 to 59). |
Second |
The second (0 to 59). |
Millisecond |
The millisecond (0 to 999). |
DayOfWeek |
The day of the week. |
DayOfYear |
The day of the year (1 to 366). |
Date |
The date at 00:00 (the same zone). |
TimeOfDay |
The time since 00:00. |
Add |
The date and time span later (earlier for a negative span). |
AddTicks |
The date and time ticks ticks of 100 ns later. |
AddDays |
The date and time days days later (fractions are possible). |
AddHours |
The date and time hours hours later. |
AddMinutes |
The date and time minutes minutes later. |
AddSeconds |
The date and time seconds seconds later. |
AddMilliseconds |
The date and time milliseconds milliseconds later. |
AddMonths |
The date months whole months later: the day stays, or becomes the last day of a shorter month (Jan 31 + 1 month is Feb 28 or 29). |
AddYears |
The date years years later (Feb 29 becomes Feb 28 in a year that is not a leap year). |
Subtract |
The time from other to this date (both are taken in UTC if one of them is). |
ToUniversalTime |
The same instant in UTC (a UTC value stays as it is). |
ToLocalTime |
The same instant in local time (a local value stays as it is). |
CompareTo |
compares the instants (a local and a UTC value are compared in UTC) |
Equals |
Whether both are the same instant and zone (IEquatable). |
GetHashCode |
A hash code of the date and time (IHashable). |
ToString |
The date and time as text: 2026-10-01 14:05:09. |
ToIsoString |
ISO 8601 with the zone: 2026-10-01T12:05:09.250Z (UTC) or 2026-10-01T14:05:09.250+02:00 (local) |
ToUnixSeconds
Section titled “ToUnixSeconds”int64 ToUnixSeconds()The seconds since 1970-01-01 00:00 UTC (Unix time); a local time is converted to UTC first.
ToUnixMilliseconds
Section titled “ToUnixMilliseconds”int64 ToUnixMilliseconds()The milliseconds since 1970-01-01 00:00 UTC; a local time is converted to UTC first.
int Year()The year (1 to 9999).
int Month()The month (1 to 12).
int Day()The day of the month (1 to 31).
int Hour()The hour (0 to 23).
Minute
Section titled “Minute”int Minute()The minute (0 to 59).
Second
Section titled “Second”int Second()The second (0 to 59).
Millisecond
Section titled “Millisecond”int Millisecond()The millisecond (0 to 999).
DayOfWeek
Section titled “DayOfWeek”DayOfWeek DayOfWeek()The day of the week.
DayOfYear
Section titled “DayOfYear”int DayOfYear()The day of the year (1 to 366).
DateTime Date()The date at 00:00 (the same zone).
TimeOfDay
Section titled “TimeOfDay”TimeSpan TimeOfDay()The time since 00:00.
DateTime Add(TimeSpan span)The date and time span later (earlier for a negative span).
AddTicks
Section titled “AddTicks”DateTime AddTicks(int64 ticks)The date and time ticks ticks of 100 ns later.
AddDays
Section titled “AddDays”DateTime AddDays(double days)The date and time days days later (fractions are possible).
AddHours
Section titled “AddHours”DateTime AddHours(double hours)The date and time hours hours later.
AddMinutes
Section titled “AddMinutes”DateTime AddMinutes(double minutes)The date and time minutes minutes later.
AddSeconds
Section titled “AddSeconds”DateTime AddSeconds(double seconds)The date and time seconds seconds later.
AddMilliseconds
Section titled “AddMilliseconds”DateTime AddMilliseconds(double milliseconds)The date and time milliseconds milliseconds later.
AddMonths
Section titled “AddMonths”DateTime AddMonths(int months)The date months whole months later: the day stays, or becomes the last day of a shorter month (Jan 31 + 1
month is Feb 28 or 29).
AddYears
Section titled “AddYears”DateTime AddYears(int years)The date years years later (Feb 29 becomes Feb 28 in a year that is not a leap year).
Subtract
Section titled “Subtract”TimeSpan Subtract(DateTime other)The time from other to this date (both are taken in UTC if one of them is).
DateTime Subtract(TimeSpan span)The date and time span earlier.
ToUniversalTime
Section titled “ToUniversalTime”DateTime ToUniversalTime()The same instant in UTC (a UTC value stays as it is). Near a change of daylight saving time, the offset of the result decides.
ToLocalTime
Section titled “ToLocalTime”DateTime ToLocalTime()The same instant in local time (a local value stays as it is).
CompareTo
Section titled “CompareTo”int CompareTo(DateTime other)compares the instants (a local and a UTC value are compared in UTC)
Equals
Section titled “Equals”bool Equals(DateTime other)Whether both are the same instant and zone (IEquatable).
GetHashCode
Section titled “GetHashCode”int GetHashCode()A hash code of the date and time (IHashable).
ToString
Section titled “ToString”string ToString()The date and time as text: 2026-10-01 14:05:09.
string ToString(string format)The date and time in a custom format: d.ToString("dd.MM.yyyy HH:mm").
| Letters | Meaning |
|---|---|
yyyy yy |
the year with 4 or 2 digits |
MMMM MMM MM M |
the month: its name, the short name, 2 digits, the number |
dddd ddd dd d |
the day: the name of the weekday, its short name, 2 digits, the number |
HH H |
the hour, 0 to 23 (2 digits or the number) |
hh h |
the hour, 1 to 12 |
mm m |
the minute |
ss s |
the second |
fff ff f |
fractions of a second |
tt |
AM or PM |
Other letters and text in '...' are copied; \x copies x. The names are English.
ToIsoString
Section titled “ToIsoString”string ToIsoString()ISO 8601 with the zone: 2026-10-01T12:05:09.250Z (UTC) or 2026-10-01T14:05:09.250+02:00 (local)