Understanding Unix Timestamp in C#: From Timestamp to DateTime and Back

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Unix Timestamp is More Than Just a Number

\n

Every developer working with APIs, databases, or logs will eventually encounter a Unix timestamp. It's an integer (or floating-point number) representing the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC — the so-called Unix epoch.

\n

But when you see the number 1719878400 in a server response, your brain can't immediately say, "Aha, that's July 2, 2024." We need to convert it into a familiar DateTime. And conversely — save a date as a timestamp for transmission.

\n

On Stack Overflow, the question "How can I convert a Unix timestamp to DateTime and vice versa?" has garnered hundreds of thousands of views. This means even experienced C# developers sometimes get confused by the nuances. In this article, we won't just provide ready-made recipes; we'll also look under the hood: we'll break down how DateTime, DateTimeOffset, ticks, and time zones work.

\n\n

What is a Unix Timestamp Really?

\n

Before writing code, let's agree on terms. A Unix timestamp is the number of seconds (sometimes milliseconds) since midnight on January 1, 1970 UTC. Importantly: it's an absolute moment in time, independent of time zone. This is precisely why it's so convenient to use in distributed systems.

\n

In C#, we work with two main types:

\n
    \n
  • DateTime — a classic type that can be either in UTC or local time (via the Kind property).
  • \n
  • DateTimeOffset — a more modern type that explicitly stores the offset from UTC. Recommended for new projects.
  • \n
\n

Under the hood, DateTime stores the number of ticks (100-nanosecond intervals) since January 1, 0001. A Unix timestamp is simply the number of seconds since 1970. To convert between them, you need to calculate the difference between these two epochs.

\n\n

Converting Unix Timestamp to DateTime (and Back) — Basic Methods

\n

Starting with .NET Framework 4.6 and .NET Core, C# introduced built-in methods that make life easier. Let's look at them.

\n\n

1. From Unix timestamp (seconds) to DateTime

\n

The simplest way is to use DateTimeOffset.FromUnixTimeSeconds(). It returns a DateTimeOffset, which can then be converted to DateTime.

\n
long unixTimestamp = 1719878400; // July 2, 2024, 00:00:00 UTC\n\n// Get DateTimeOffset\nDateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(unixTimestamp);\n\n// Convert to DateTime (UTC)\nDateTime utcDateTime = dateTimeOffset.UtcDateTime;\n\nConsole.WriteLine(utcDateTime); // 02.07.2024 0:00:00
\n

If you need local time (e.g., for displaying to the user), use dateTimeOffset.LocalDateTime. But be careful: local time depends on the server or computer's settings.

\n\n

2. From DateTime to Unix timestamp

\n

The reverse conversion uses the ToUnixTimeSeconds() method on DateTimeOffset.

\n
DateTime utcDateTime = new DateTime(2024, 7, 2, 0, 0, 0, DateTimeKind.Utc);\n\n// Create DateTimeOffset with explicit UTC\nDateTimeOffset dateTimeOffset = new DateTimeOffset(utcDateTime);\n\nlong unixTimestamp = dateTimeOffset.ToUnixTimeSeconds();\n\nConsole.WriteLine(unixTimestamp); // 1719878400
\n

It is critically important: always specify DateTimeKind.Utc when creating a DateTime that you intend to convert. If you don't, DateTimeOffset will assume the time is local, and you'll get an incorrect timestamp — shifted by your time zone.

\n\n

3. Working with milliseconds

\n

Sometimes timestamps are provided in milliseconds (e.g., from JavaScript). For this,

Blogs

Book Recommendations