Unix Timestamp in C#

C# uses DateTimeOffset.UtcNow for timezone-safe timestamps. ToUnixTimeSeconds() and ToUnixTimeMilliseconds() give you Unix timestamps directly — no math required.

Code Examples

Current timestamp (seconds)

long seconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

DateTimeOffset is timezone-aware and the recommended type for Unix timestamps in modern C#. Returns long.

Current timestamp (milliseconds)

long ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

Returns milliseconds since epoch as long. Available since .NET 4.6 / .NET Core 1.0.

Convert timestamp to DateTimeOffset

var dto = DateTimeOffset.FromUnixTimeSeconds(timestamp);

Converts a Unix timestamp (seconds) back to a DateTimeOffset in UTC. Use .LocalDateTime to convert to local time.

Using DateTime (legacy — avoid for timestamps)

var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long seconds = (long)(DateTime.UtcNow - epoch).TotalSeconds;

The old manual approach before .NET 4.6. Included for reference — prefer DateTimeOffset.ToUnixTimeSeconds() in new code.

Format as ISO 8601

var dto = DateTimeOffset.FromUnixTimeSeconds(timestamp);
string iso = dto.ToString("o"); // Round-trip format

The "o" format specifier produces ISO 8601 / RFC 3339 output: 2024-02-22T00:00:00.0000000+00:00.

Note

Always use DateTimeOffset instead of DateTime for Unix timestamps — DateTimeOffset carries timezone offset information, preventing local-time bugs. DateTime.Kind = Unspecified (the default) is a common source of subtle timestamp errors in C# apps.

Main Epoch Converter

Use the homepage when you need epoch to date, date to epoch, or a full epoch converter

This C# guide shows how to create and parse Unix timestamps in code. If your next step is broader search intent like epoch converter, epoch to date, epoch time to date, or unix epoch converter, jump back to the live homepage tools to paste a raw Unix value, auto-detect seconds versus milliseconds, and copy the readable answer instantly.

Related Tools

More free tools for developers debugging timestamps, schedules, and text changes.