C# Reference ยท Date, Time and Globalization
Unix time in C#
Convert between Unix timestamps and dates in C# with DateTimeOffset.FromUnixTimeSeconds, ToUnixTimeMilliseconds, DateTime.UnixEpoch, ranges and the year 2038 issue.
Unix time counts the seconds (or milliseconds) that have passed since the Unix epoch, 00:00:00 UTC on 1 January 1970, ignoring leap seconds. It is a compact, zone-free way to describe an instant, so it appears in JSON APIs, JWT tokens, databases and log formats.
In .NET, DateTimeOffset has the conversion methods: FromUnixTimeSeconds, FromUnixTimeMilliseconds, ToUnixTimeSeconds and ToUnixTimeMilliseconds. DateTime and DateTimeOffset also expose UnixEpoch as a constant for arithmetic.
long seconds = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUnixTimeSeconds(); DateTimeOffset back = DateTimeOffset.FromUnixTimeSeconds(seconds);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static DateTimeOffset FromUnixTimeSeconds(long seconds); public static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds); public long ToUnixTimeSeconds(); public long ToUnixTimeMilliseconds(); public static readonly DateTimeOffset UnixEpoch; // also DateTime.UnixEpoch (Kind = Utc)
| Member | Description |
|---|---|
FromUnixTimeSeconds(seconds) |
The instant seconds after the epoch, as a DateTimeOffset with offset zero. |
FromUnixTimeMilliseconds(ms) |
Same with milliseconds. |
ToUnixTimeSeconds() |
Whole seconds since the epoch; the fraction is truncated toward earlier time. |
ToUnixTimeMilliseconds() |
Whole milliseconds since the epoch. |
UnixEpoch |
1970-01-01T00:00:00 UTC. |
Errors: the From methods throw ArgumentOutOfRangeException when the value is outside the range of DateTimeOffset (years 1 to 9999).
Usage notes
- The result of
From...has offset zero. UseToOffset(...)orTimeZoneInfo.ConvertTimefor a local presentation. - Truncation, not rounding.
ToUnixTimeSeconds()drops the fractional second, so10:00:00.999gives the same value as10:00:00.000. Use the millisecond version when you need to keep the fraction. - Seconds or milliseconds? JavaScript's
Date.now()and Java'sSystem.currentTimeMillis()use milliseconds; many Unix tools and JWTexpclaims use seconds. A value near 1.8 billion is seconds, one near 1.8 trillion is milliseconds. Mixing them up gives a date in January 1970 (seconds read as milliseconds) or anArgumentOutOfRangeException(milliseconds read as seconds). - Always use
long. A signed 32-bit seconds counter overflows at 2038-01-19 03:14:07 UTC; .NET returns 64-bit values, so it only matters if you cast tointor hand the value to a 32-bit system. DateTimehas no direct method. SubtractDateTime.UnixEpochfrom a UTC value. Passing a localDateTimegives a wrong result, so callToUniversalTime()first.- Leap seconds are ignored. Unix time treats every day as exactly 86,400 seconds, the same as
DateTime. - Current timestamp:
DateTimeOffset.UtcNow.ToUnixTimeSeconds()(the value is clock-dependent, so it is not printed in the examples here). For tests, inject aTimeProvider. - Ticks are different.
DateTime.Tickscounts 100 ns units since 0001-01-01, not since 1970.