Skip to content
We just launched Duende IdentityServer v7.2.0 and BFF v3.0. Check it out!

Epoch Time Conversion

JSON Web Token (JWT) tokens use so-called Epoch or Unix time to represent date/times, which is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT).

In .NET, you can convert DateTimeOffset to Unix time via the two methods of ToUnixTimeSeconds and ToUnixTimeMilliseconds:

EpochTimeExamples.cs
var seconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var milliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

IdentityModel contains extensions methods for DateTime to convert to/from Unix time:

DateTimeExtensionExample.cs
var dt = DateTime.UtcNow;
// The time returned is in seconds
var unix = dt.ToEpochTime();