Epoch & Unix Timestamp in C#

Get the current epoch timestamp

long epochSeconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

Convert epoch to date

DateTimeOffset dto = DateTimeOffset.FromUnixTimeSeconds(1718200000);

Convert date to epoch

long epoch = new DateTimeOffset(2026, 6, 12, 0, 0, 0, TimeSpan.Zero).ToUnixTimeSeconds();

C# notes

Use DateTimeOffset, not DateTime — it carries the offset explicitly. ToUnixTimeMilliseconds() gives milliseconds. DateTime.Ticks are 100-ns units since year 1, not Unix time.

Check any value live with the epoch converter, or read what Unix time actually counts.

Other languages

C · C++ · Dart · Go · Java · JavaScript · Kotlin · MySQL · Perl · PHP · PostgreSQL · Python · R · Ruby · Rust · Scala · SQLite · Swift · TypeScript