Unix timestamps in C#
Everything C# needs for Unix timestamps: the current epoch time in seconds and milliseconds, timestamp-to-date and date-to-timestamp conversion, and ISO 8601 parsing. C# has two timestamp-shaped numbers: Unix seconds via DateTimeOffset, and DateTime.Ticks, which counts from year 1 and is 8 orders of magnitude away. Confuse them and the date is nonsense. There's a live converter at the bottom for checking any value the snippets produce.
Get the current Unix timestamp
long seconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); // 1700000000
long millis = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); // 1700000000000Convert a timestamp to a DateTimeOffset
var dto = DateTimeOffset.FromUnixTimeSeconds(1700000000);
// 2023-11-14 22:13:20 +00:00Convert a DateTime to a timestamp
long seconds = new DateTimeOffset(dateTime).ToUnixTimeSeconds();This throws for DateTimeKind.Unspecified values that would produce an ambiguous offset; specify the kind or use a DateTimeOffset throughout.
Parse an ISO 8601 string
var dto = DateTimeOffset.Parse("2023-11-14T22:13:20Z",
CultureInfo.InvariantCulture);Always pass InvariantCulture for machine formats; the default culture can reorder day and month.
Check a value
Detected: Unix timestamp (seconds)
Your timezone
UTC · GMT+00:00 · UTC
Tue, Nov 14, 2023, 10:13:20 PM
Common formats
UTC
Tue, Nov 14, 2023, 10:13:20 PM
ISO 8601
2023-11-14T22:13:20.000Z
Unix (seconds)
1700000000
Unix (millis)
1700000000000
Relative
…
World clock
Los Angeles PST
Tue, Nov 14, 2023, 02:13:20 PM
GMT-08:00
Chicago CST
Tue, Nov 14, 2023, 04:13:20 PM
GMT-06:00
New York EST
Tue, Nov 14, 2023, 05:13:20 PM
GMT-05:00
London GMT
Tue, Nov 14, 2023, 10:13:20 PM
GMT+00:00
Paris CET
Tue, Nov 14, 2023, 11:13:20 PM
GMT+01:00
Kolkata IST
Wed, Nov 15, 2023, 03:43:20 AM
GMT+05:30
Shanghai CST
Wed, Nov 15, 2023, 06:13:20 AM
GMT+08:00
Tokyo JST
Wed, Nov 15, 2023, 07:13:20 AM
GMT+09:00
Sydney AEDT
Wed, Nov 15, 2023, 09:13:20 AM
GMT+11:00
Common questions
- What's the difference between Ticks and a Unix timestamp?
- DateTime.Ticks counts 100-nanosecond intervals since 0001-01-01, an 18-digit number. Unix time counts seconds since 1970, about 10 digits. If you have an 18-digit value, decode it with the ticks-to-datetime converter on this site.
- Should I use DateTime or DateTimeOffset?
- DateTimeOffset for anything that represents a real moment: it carries its UTC offset, so it round-trips safely. DateTime's Kind property is too easy to lose in serialization.
- How do I get the timestamp of a specific date?
- new DateTimeOffset(2023, 11, 14, 22, 13, 20, TimeSpan.Zero).ToUnixTimeSeconds(). The TimeSpan is the UTC offset of the wall time you're describing.