A Unix timestamp is the number of seconds that have passed since 00:00:00 UTC on 1 January 1970, known as the Unix epoch. It is a single integer with no time zone attached, which makes it an easy way to store and compare moments in time. The main traps are mixing up seconds and milliseconds, treating local time as UTC, and the 32-bit limit that overflows on 19 January 2038.
What a Unix timestamp is
The value 0 means 1970-01-01T00:00:00Z. The value 1700000000 means 2023-11-14T22:13:20Z, and every extra second adds one to the number. Because it counts from a fixed instant in UTC, two systems anywhere in the world that read the same timestamp agree on the exact moment, even if they would display it differently on a wall clock.
That is why timestamps suit databases, logs, APIs and token expiry fields: comparing two moments is plain integer comparison, and subtracting them gives a duration in seconds. If you work with signed tokens, the exp and iat claims in a JWT are Unix timestamps in seconds; the JWT decoder is one place you will meet them.
Seconds or milliseconds: count the digits
Classic Unix time counts seconds. JavaScript's Date.now(), Java's System.currentTimeMillis() and many web APIs count milliseconds. The two differ by a factor of 1000, and the quickest check is the length of the number. For dates from September 2001 until November 2286, a timestamp in seconds has 10 digits and one in milliseconds has 13.
| Unit | Example | Digits |
|---|---|---|
| Seconds | 1700000000 | 10 |
| Milliseconds | 1700000000123 | 13 |
The consequence of getting this wrong is dramatic rather than subtle. Feeding 1700000000 (seconds) to JavaScript's Date, which expects milliseconds, gives 1970-01-20T16:13:20.000Z. Feeding a millisecond value to something that expects seconds yields a date tens of thousands of years away, which many libraries reject or overflow on.
Converting: worked examples
All of the outputs below were produced by running the commands and are for the same instant, 1700000000.
JavaScript
const ts = 1700000000; // seconds
new Date(ts * 1000).toISOString(); // "2023-11-14T22:13:20.000Z"
new Date(1700000000123).toISOString(); // "2023-11-14T22:13:20.123Z"
Math.floor(Date.now() / 1000); // current time in seconds
Math.floor(Date.UTC(2024, 0, 1) / 1000); // 1704067200 (2024-01-01T00:00:00Z; note months are 0-based)
Python
from datetime import datetime, timezone
datetime.fromtimestamp(1700000000, tz=timezone.utc).isoformat()
# '2023-11-14T22:13:20+00:00'
datetime(2024, 1, 1, tzinfo=timezone.utc).timestamp()
# 1704067200.0
Passing tz=timezone.utc matters. Without it, fromtimestamp returns a naive datetime in the machine's local zone, which looks right on your laptop and wrong on a server in another region.
Shell
# GNU date (Linux)
date -u -d @1700000000 +%Y-%m-%dT%H:%M:%SZ
# BSD date (macOS)
date -u -r 1700000000 +%Y-%m-%dT%H:%M:%SZ
# both print: 2023-11-14T22:13:20Z
date +%s # current timestamp in seconds
For one-off conversions without writing code, the timestamp converter on this site handles both seconds and milliseconds.
Time zones: store UTC, display local
A timestamp has no time zone, because it identifies one instant. The same instant, 1700000000, reads as 22:13 on 14 November in UTC, 17:13 the same day in New York (UTC-5 in November), and 07:13 on 15 November in Tokyo (UTC+9). Different clocks, same moment.
So the sound pattern is to store and transmit UTC, whether as a timestamp or an ISO 8601 string ending in Z, and convert to the viewer's zone only at the last step, when rendering. Storing local times invites trouble with daylight saving changes, where one local hour repeats in autumn and another is skipped in spring.
Leap seconds
Unix time pretends every day has exactly 86,400 seconds. Real UTC occasionally inserts a leap second, but Unix time does not count it; systems typically repeat or smear a second instead. For almost all applications this makes no difference. It only matters if you need to compute precise elapsed physical time across a leap second, in which case you need a specialised time scale rather than Unix time.
The Year 2038 problem
Many older systems store the timestamp as a signed 32-bit integer. The largest value that type can hold is 2,147,483,647, which is 2038-01-19T03:14:07Z. One second later the value wraps to the most negative number, -2,147,483,648, which is interpreted as 1901-12-13T20:45:52Z. Software then sees dates jump backwards by about 136 years.
Modern 64-bit systems and languages are not affected in practice, since a 64-bit counter of seconds lasts for hundreds of billions of years. The risk sits in places where a narrow type is baked in: legacy C code using a 32-bit time_t, embedded devices, old file formats, binary protocols, and database columns declared as 32-bit integers. An unsigned 32-bit value extends the limit to 2106-02-07T06:28:15Z, but that merely delays the problem and cannot represent dates before 1970.
The fix is to use a 64-bit integer or a proper timestamp type, and to test with dates beyond 2038. Anything that computes expiry dates or long-term schedules can hit the limit years before the date itself arrives.
Common mistakes
- Mixing seconds and milliseconds. Check the digit count and the documentation of the API. When in doubt, name variables with a unit, such as
expires_at_sorcreated_ms. - Parsing local time as UTC, or the reverse. A string like
2023-11-14 22:13:20has no zone. Add an explicitZor offset before converting, and never assume the server's zone equals the user's. - Storing timestamps as strings. Text sorts and compares incorrectly across formats and wastes space. Use an integer or a native date-time type.
- Using 32-bit columns. Use a 64-bit integer (for example
BIGINT) for new schemas. - Floating-point precision. Millisecond timestamps fit safely in a JavaScript number, but microsecond or nanosecond values do not; use integers or BigInt for those.
For a related look at how data formats affect what you can safely store, see JSON vs YAML.
Frequently Asked Questions
Is a Unix timestamp always in UTC?
Yes. It counts elapsed seconds since the UTC epoch, so it carries no zone. Zones only come into play when you format it for display.
How do I know if a timestamp is in seconds or milliseconds?
Count the digits. Ten digits is almost certainly seconds and thirteen is milliseconds for present-day dates. Converting and checking whether the result is a plausible date confirms it.
Will the Year 2038 problem crash everything?
No. Systems using 64-bit time are unaffected. Only software or storage that still uses signed 32-bit time is at risk, and it should be found and migrated.
Can a Unix timestamp be negative?
Yes. Negative values represent moments before 1970-01-01T00:00:00Z. Not every library or database handles them, so test if you need historical dates.
Comments
Leave a Comment
No comments yet. Be the first to comment!