Skip to content
CShift0.32

DateTime

Struct · namespace System · Since 0.19

struct DateTime : IComparable<DateTime>, IEquatable<DateTime>, IHashable

A 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:00

A 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.

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 Ticks

The ticks of 100 ns since 0001-01-01 00:00.

bool IsUtc

Whether the time is UTC (otherwise it is local time).

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).
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

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.

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.

static DateTime FromUnixSeconds(int64 seconds)

The UTC date and time seconds seconds after 1970-01-01 00:00 UTC (Unix time).

static DateTime FromUnixMilliseconds(int64 milliseconds)

The UTC date and time milliseconds milliseconds after 1970-01-01 00:00 UTC.

static bool IsLeapYear(int year)

Whether year is a leap year in the Gregorian calendar.

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

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)
int64 ToUnixSeconds()

The seconds since 1970-01-01 00:00 UTC (Unix time); a local time is converted to UTC first.

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).

int Minute()

The minute (0 to 59).

int Second()

The second (0 to 59).

int Millisecond()

The millisecond (0 to 999).

DayOfWeek DayOfWeek()

The day of the week.

int DayOfYear()

The day of the year (1 to 366).

DateTime Date()

The date at 00:00 (the same zone).

TimeSpan TimeOfDay()

The time since 00:00.

DateTime Add(TimeSpan span)

The date and time span later (earlier for a negative span).

DateTime AddTicks(int64 ticks)

The date and time ticks ticks of 100 ns later.

DateTime AddDays(double days)

The date and time days days later (fractions are possible).

DateTime AddHours(double hours)

The date and time hours hours later.

DateTime AddMinutes(double minutes)

The date and time minutes minutes later.

DateTime AddSeconds(double seconds)

The date and time seconds seconds later.

DateTime AddMilliseconds(double milliseconds)

The date and time milliseconds milliseconds later.

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).

DateTime AddYears(int years)

The date years years later (Feb 29 becomes Feb 28 in a year that is not a leap year).

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.

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.

DateTime ToLocalTime()

The same instant in local time (a local value stays as it is).

int CompareTo(DateTime other)

compares the instants (a local and a UTC value are compared in UTC)

bool Equals(DateTime other)

Whether both are the same instant and zone (IEquatable).

int GetHashCode()

A hash code of the date and time (IHashable).

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.

string ToIsoString()

ISO 8601 with the zone: 2026-10-01T12:05:09.250Z (UTC) or 2026-10-01T14:05:09.250+02:00 (local)