A Unix timestamp is a single number that identifies a moment in time: the count of seconds since 00:00:00 UTC on 1 January 1970, a date called the epoch. It is how most systems store and compare times, because a number is easy to sort, subtract and index.
It is also the source of a steady stream of bugs, and nearly all of them come from three questions that the number itself does not answer: what unit is it in, what zone is it in, and what happens when it gets large?
Seconds or milliseconds?
Both are in common use, and the number does not label itself.
| System | Unit |
|---|---|
Unix time(), PHP time(), Python time.time() |
Seconds (Python returns a float) |
JavaScript Date.now(), Java System.currentTimeMillis() |
Milliseconds |
JWT exp, iat and nbf claims |
Seconds |
Mix them up and the result is wildly wrong, but not obviously so. Take 1700000000:
| Read as | Date |
|---|---|
| Seconds | 14 November 2023, 22:13:20 UTC |
| Milliseconds | 20 January 1970, 16:13:20 UTC |
And the reverse, 1700000000000, read as seconds, lands in the year 55840.
The quickest way to tell which unit you have is to count digits. Right now, a timestamp in seconds has 10 digits, and one in milliseconds has 13. Microseconds have 16 and nanoseconds 19. Ten digits will stay accurate for a long time: 9999999999 seconds is 20 November 2286.
In JavaScript, new Date(1700000000) is the classic trap. The constructor takes milliseconds, so passing seconds gives a date in January 1970. Multiply by 1000 first. The Unix timestamp converter accepts either form and shows what each interpretation means, which makes a wrong unit easy to spot.
Getting and converting one
The current time in seconds, and converting a value back to a readable date, in the places you are most likely to need it:
date +%s # now, in seconds (Linux and macOS)
date -u -d @1700000000 # to a date, GNU/Linux
date -u -r 1700000000 # to a date, macOS and BSD
Math.floor(Date.now() / 1000) // now, in seconds
new Date(1700000000 * 1000).toISOString() // "2023-11-14T22:13:20.000Z"
In Python, int(time.time()) gives the current time in seconds. Whatever the language, the habit is the same: decide the unit first, then convert at the boundary.
A timestamp has no time zone
This surprises people. A timestamp identifies one absolute instant, the same everywhere on Earth. There is no "UTC timestamp" and "local timestamp". The zone only appears when you display the number as a calendar date.
So the common failure is not in storing timestamps but in converting them. A job that formats a date using the server's local zone prints a different wall-clock time on a server in another region, though the timestamp is identical. Store timestamps, or ISO 8601 strings that include an offset such as 2026-11-09T14:30:00Z, and convert to a local zone only at the edge, when a human is about to read it. Avoid storing naive local date strings with no zone at all. They cannot be interpreted correctly later, especially around daylight saving changes.
Unix time also ignores leap seconds. It treats every day as exactly 86,400 seconds, which is part of why it is simple to calculate with. Wall-clock time and Unix time can drift apart by a few seconds over the years, and for almost every application that does not matter. The POSIX definition is precise about how seconds since the epoch are counted.
The 2038 problem
Many older systems stored the timestamp in a signed 32-bit integer. The largest value such an integer can hold is 2,147,483,647, and as a timestamp that is:
2147483647 → 2038-01-19 03:14:07 UTC
One second later the counter overflows. In a signed 32-bit integer it wraps to its most negative value, which reads as:
-2147483648 → 1901-12-13 20:45:52 UTC
A system that stores time this way would suddenly believe it is 1901. This is the "year 2038 problem", a cousin of the Y2K bug.
Most modern software uses 64-bit time and will not notice. The risk lives in older embedded devices, file formats and database schemas that fixed the width of the field long ago. One well-known example is MySQL's TIMESTAMP column type, whose documented range ends at 03:14:07 UTC on 19 January 2038. Its DATETIME type does not share that limit. If you maintain a system that stores dates far into the future, such as expiry dates, a 30-year mortgage or a long certificate lifetime, you may meet the boundary well before 2038 arrives.
To find out whether a system is affected, store a date after 2038 in it and read it back. It is a two-minute test.
Practical habits
- Name the unit. Call a field
created_at_msorexpires_in_seconds, nottimestamp. - Use 64-bit integers for stored timestamps.
- Keep the stored value in UTC and convert only for display.
- Check the digit count whenever a date looks absurd. A year in the 1970s or the 55,000s means the unit is wrong.
- Be explicit at API boundaries. Document whether a value is in seconds or milliseconds.
Timestamps are also embedded in other formats. UUID version 7 begins with a 48-bit millisecond timestamp, which is why those identifiers sort by creation time (see UUID v7 vs v4 as a database primary key). Scheduled jobs depend on the same clock assumptions, and the cron expression guide covers the time zone traps there. Tokens carry timestamps too, and the JWT decoder converts the exp and iat claims into readable dates for you.