Unix timestamps in C#
Since .NET Framework 4.6 and in every version of .NET Core, DateTimeOffset has Unix time built in. Prefer it to DateTime, whose Kind property is the source of most mistakes.
Current timestamp
Console.WriteLine(DateTimeOffset.UtcNow.ToUnixTimeSeconds()); // → e.g. 1763164800
Console.WriteLine(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()); // → e.g. 1763164800123Both return a long. UtcNow and Now give the same timestamp, because a DateTimeOffset knows its own offset.
Timestamp to date
Every example uses 1700000000, which is 14 November 2023 at 22:13:20 UTC, so you can compare the output across languages or check it in the timestamp converter.
DateTimeOffset d = DateTimeOffset.FromUnixTimeSeconds(1700000000);
Console.WriteLine(d.ToString("yyyy-MM-ddTHH:mm:ssZ")); // → 2023-11-14T22:13:20Z
Console.WriteLine(d.UtcDateTime.ToString("u")); // → 2023-11-14 22:13:20Z
TimeZoneInfo newYork = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"); // "America/New_York" on Linux and macOS
Console.WriteLine(TimeZoneInfo.ConvertTime(d, newYork).ToString("yyyy-MM-dd HH:mm:ss zzz")); // → 2023-11-14 17:13:20 -05:00
Console.WriteLine(DateTimeOffset.FromUnixTimeMilliseconds(1700000000123).ToString("HH:mm:ss.fff")); // → 22:13:20.123FromUnixTimeSeconds returns a value with offset zero. Windows uses its own zone names (Eastern Standard Time); on Linux and macOS use IANA names. From .NET 6, either kind of name works on any system that has ICU.
Date to timestamp
Console.WriteLine(new DateTimeOffset(2023, 11, 14, 22, 13, 20, TimeSpan.Zero).ToUnixTimeSeconds()); // → 1700000000
Console.WriteLine(DateTimeOffset.Parse("2023-11-14T22:13:20Z", CultureInfo.InvariantCulture).ToUnixTimeSeconds()); // → 1700000000Parse with CultureInfo.InvariantCulture, or the result depends on the regional settings of the machine. These snippets need using System; and using System.Globalization;.
Pitfalls
DateTime unspecified = new DateTime(2023, 11, 14, 22, 13, 20); // Kind is Unspecified: conversions treat it as local time
DateTime utc = DateTime.SpecifyKind(unspecified, DateTimeKind.Utc);
Console.WriteLine(new DateTimeOffset(utc).ToUnixTimeSeconds()); // → 1700000000
Console.WriteLine((long)(utc - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds); // → 1700000000 before .NET 4.6DateTimeKind.Unspecified. ADateTimebuilt from parts, or read from most databases, has no kind, andnew DateTimeOffset(dt)orToUniversalTime()then assume it is local time. CallDateTime.SpecifyKindfirst.- Old frameworks. Before .NET Framework 4.6 there is no
ToUnixTimeSeconds; subtract the epoch as in the last line. On .NET Core 2.1 and later the epoch is available asDateTime.UnixEpoch. - Ticks are not Unix time.
DateTime.Tickscounts 100-nanosecond steps from the year 0001. - Range.
FromUnixTimeSecondsthrowsArgumentOutOfRangeExceptionoutside the years 0001 to 9999, which is what happens when milliseconds are passed as seconds.
Questions
How do I get the Unix timestamp in C#?
DateTimeOffset.UtcNow.ToUnixTimeSeconds() for seconds or ToUnixTimeMilliseconds() for milliseconds. Both exist from .NET Framework 4.6 and in all versions of .NET Core and .NET 5 and later.
How do I convert a Unix timestamp to DateTime?
DateTimeOffset.FromUnixTimeSeconds(ts).UtcDateTime gives a DateTime with Kind set to Utc. Use .LocalDateTime for the machine's local time.
Why is my converted time off by my UTC offset?
A DateTime with Kind Unspecified was treated as local time. Mark it as UTC with DateTime.SpecifyKind(value, DateTimeKind.Utc) before converting.