Skip to content
Tutorials Center
My learning

Topics

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.

C#
long seconds = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero).ToUnixTimeSeconds();
DateTimeOffset back = DateTimeOffset.FromUnixTimeSeconds(seconds);

Try it

C# Timestamps to dates and back

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.

C# DateTime, ranges and the year 2038

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

C#
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. Use ToOffset(...) or TimeZoneInfo.ConvertTime for a local presentation.
  • Truncation, not rounding. ToUnixTimeSeconds() drops the fractional second, so 10:00:00.999 gives the same value as 10:00:00.000. Use the millisecond version when you need to keep the fraction.
  • Seconds or milliseconds? JavaScript's Date.now() and Java's System.currentTimeMillis() use milliseconds; many Unix tools and JWT exp claims 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 an ArgumentOutOfRangeException (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 to int or hand the value to a 32-bit system.
  • DateTime has no direct method. Subtract DateTime.UnixEpoch from a UTC value. Passing a local DateTime gives a wrong result, so call ToUniversalTime() 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 a TimeProvider.
  • Ticks are different. DateTime.Ticks counts 100 ns units since 0001-01-01, not since 1970.