Unix Timestamps: Seconds, Time Zones, 2038
Last updated: 2026-11-05
Timestamp showing 1970, off by hours or thousands of years ahead? Tested code explains seconds vs milliseconds, time zones and date parsing.
What a timestamp is
A Unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. It marks a moment and has no time zone of its own, so the same moment is the same number everywhere. For example, 1700000000 is 2023-11-14 22:13:20 UTC.
Problem 1: mixing up seconds and milliseconds
Current timestamps have 10 digits in seconds and 13 in milliseconds. The symptoms of mixing them up are easy to spot:
| Symptom | Cause | Example (JavaScript) |
|---|---|---|
| The date is in 1970 | Seconds used as milliseconds | new Date(1700000000) gives 1970-01-20 |
| The date is thousands of years ahead | Milliseconds used as seconds | new Date(1700000000000 * 1000) gives the year 55840 |
Languages differ. JavaScript and Java commonly use milliseconds, while Python’s time.time() and the Unix command line use seconds:
// JavaScript: Date.now() is in milliseconds const seconds = Math.floor(Date.now() / 1000); const date = new Date(seconds * 1000); # Python: time.time() is in seconds import time from datetime import datetime, timezone seconds = int(time.time()) date = datetime.fromtimestamp(seconds, timezone.utc) # Linux and macOS: current timestamp in seconds date +%s
Problem 2: off by a few hours
If a result is off by a whole number of hours, UTC and local time have usually been mixed up. The timestamp is fine. The problem is which time zone was used to turn it into a date. Two cases are especially common:
- UTC is shown as if it were local time. The Z at the end of toISOString() output means UTC, not local time.
- The server and your computer have different time zones. Python’s datetime.fromtimestamp(ts) uses the system time zone when none is given, so the result changes when the code moves into a container set to UTC.
The fix is to state the time zone explicitly when converting, rather than relying on the environment’s default.
Problem 3: similar date strings, different results
JavaScript’s date parsing has rules that are easy to trip over, because formats that look alike mean different things. These results were produced in a UTC+8 environment:
new Date('2024-01-15')
// Date only: parsed as UTC, 2024-01-15T00:00:00.000Z
new Date('2024-01-15 10:00:00')
// No time zone: parsed as local time, 2024-01-15T02:00:00.000Z
new Date('2024-01-15T10:00:00Z')
// Explicit UTC, 2024-01-15T10:00:00.000Z
new Date('2024-01-15T10:00:00+08:00')
// Explicit UTC+8, 2024-01-15T02:00:00.000ZThe second format, with a space, isn’t a standard one either, so it isn’t guaranteed to parse everywhere. ISO 8601 with a time zone is the safest way to pass dates around.
Storing times and designing APIs
- Store UTC timestamps or times with a time zone, and convert to local time only for display.
- Say in your API documentation whether timestamps are in seconds or milliseconds.
- Consider putting the unit in the field name, such as created_at_ms.
The year 2038 problem
Systems that store seconds in a signed 32-bit integer can reach 2147483647 at most, which is 2038-01-19 03:14:07 UTC, and then overflow. 64-bit systems and JavaScript’s Date aren’t affected. Watch out for older embedded devices, legacy database columns and file formats that use 32-bit times. MySQL’s TIMESTAMP column, for example, ends at this moment.
Dates before 1970
These are negative numbers. For example, -86400 is 1969-12-31 00:00:00 UTC. When debugging, paste a timestamp into the Unix Timestamp Converter. It detects seconds or milliseconds and shows both local time and UTC.