Unix timestamps: seconds vs milliseconds, time zones and the year 2038
What a Unix timestamp counts, how to tell seconds from milliseconds at a glance, where time zones come in, and what breaks in 2038.
A Unix timestamp is the number of seconds that have passed since 00:00:00 UTC on 1 January 1970, a moment known as the Unix epoch. It is how most databases, log files, APIs and operating systems store "when". The format is simple, but a few details cause most of the bugs: the unit, the time zone, and the size of the integer that holds it.
One number, the same everywhere
The most useful property of a Unix timestamp is that it does not depend on where you are. The timestamp 1725436800 refers to one instant. In London it was 09:00 on 4 September 2024, in Shanghai 16:00, and in San Francisco 01:00. All three are the same moment, and all three people would record the same number.
That is why timestamps are good for storage and comparison: subtracting two of them gives the elapsed seconds directly, sorting them sorts events in time order, and there is no ambiguity about which time zone was meant. Time zones only enter the picture when you turn a timestamp into a date and time for a person to read.
Seconds or milliseconds?
The classic Unix timestamp counts seconds. JavaScript's Date.now(), Java's System.currentTimeMillis() and many JSON APIs count milliseconds instead. Mixing them up is one of the most common date bugs, and it produces dates that are wildly wrong rather than subtly wrong:
- Reading a seconds value as milliseconds:
new Date(1725436800)gives 20 January 1970. If a date shows up in January 1970, this is almost always the reason. - Reading a milliseconds value as seconds gives a date tens of thousands of years in the future, or an overflow error.
The quick test is to count digits. Present-day timestamps in seconds have 10 digits (they passed 1,000,000,000 on 9 September 2001 and will not reach 11 digits until the year 2286). In milliseconds they have 13 digits. Sixteen digits means microseconds and nineteen means nanoseconds, which some databases and tracing systems use.
The Timestamp Converter uses the same magnitude rule to detect seconds or milliseconds automatically.
Where time zones come in
A timestamp has no time zone, but every human-readable date does, whether it is shown or not. When code formats a timestamp without saying which zone to use, it uses the zone of the machine running the code. That is how the same log line shows different times on a laptop in Berlin and on a server in Virginia, and how a test that passes locally fails in CI.
Two habits avoid most of these problems:
- Store and send UTC, either as a timestamp or as an ISO 8601 string with an explicit offset, such as
2024-09-04T08:00:00Z. The trailingZmeans UTC. - Convert to local time only at the edge, when the value is shown to a person, and say which zone you used.
A converter faces the same question. This site's converter shows results in one fixed offset instead of your computer's zone, so the same input always gives the same output and a result you share with a colleague means the same thing on their screen.
Daylight saving time
Daylight saving does not affect timestamps at all. The count of seconds moves forward steadily through every clock change. It affects only the conversion to local time, and it does so in two awkward ways. When the clocks go forward, one local hour does not exist. When they go back, one local hour happens twice, so a local time such as 01:30 matches two different timestamps.
That is one more reason to schedule and store in UTC. "Run at 02:30 local time" can run twice or not at all on the night the clocks change.
Leap seconds
Unix time pretends leap seconds do not exist. Every day is exactly 86,400 seconds long in Unix time, even the few days in history that had 86,401. Systems absorb the difference by repeating a second or by "smearing" it over several hours. For almost all applications this does not matter, but it means the difference between two Unix timestamps can be off by a few seconds of true physical time across long spans.
The year 2038 problem
Many older systems store the timestamp as a signed 32-bit integer. The largest value such an integer can hold is 2147483647, which corresponds to 03:14:07 UTC on 19 January 2038. One second later, the counter overflows and wraps to a large negative number, which those systems will read as a date in December 1901.
Modern 64-bit operating systems, languages and databases use 64-bit timestamps and are not affected. The risk lies in places that are hard to update: embedded devices, old file formats, database columns declared as 32-bit integers, and binary protocols with a fixed 4-byte time field. Software that already handles dates far in the future, such as 30-year mortgage schedules, can run into the limit today.
Quick reference
0is 1970-01-01 00:00:00 UTC.- 10 digits means seconds, 13 milliseconds, 16 microseconds, 19 nanoseconds.
- A date in January 1970 almost always means seconds were read as milliseconds.
- To convert seconds to a JavaScript
Date, multiply by 1000:new Date(seconds * 1000). 2147483647is the last second a signed 32-bit timestamp can hold: 2038-01-19 03:14:07 UTC.