What is a Unix Timestamp?
A Unix timestamp (also called Epoch time or POSIX time) is simply the number of seconds since January 1, 1970, 00:00:00 UTC. That date is called the "Unix Epoch."
Right now: 1706745600
Meaning: Fri Jan 31 2024 00:00:00 UTC
No timezones. No formatting. Just a number that every system understands.
Why 1970?
Unix was being developed in the late 1960s. The engineers needed a starting point for time and picked a round number close to when they were working. January 1, 1970 was convenient—no one was going to need dates from the 1960s in their software (they thought).
Format Variations
| Format | Example | Notes |
|---|---|---|
| Seconds | 1706745600 | Standard Unix timestamp |
| Milliseconds | 1706745600000 | JavaScript, Java |
| Microseconds | 1706745600000000 | High-precision systems |
| Nanoseconds | 1706745600000000000 | Go, some databases |
JavaScript's Date.now() returns milliseconds, not seconds. Divide by 1000 to get Unix time:
Math.floor(Date.now() / 1000)
Where You'll See This
- APIs - Created/updated timestamps in responses
- Databases - Storing dates as integers (fast, compact)
- JWTs -
iat(issued at),exp(expires) claims - Logs - Timestamped events
- Caching - Cache expiration times
- Git - Commit timestamps
Advantages
| Benefit | Why It Matters |
|---|---|
| Timezone-neutral | Store once, display in any timezone |
| Compact | 10 digits vs "2024-01-31T12:00:00.000Z" |
| Sortable | Just compare numbers |
| Math-friendly | Add/subtract seconds easily |
| Universal | Every language supports it |
The Year 2038 Problem
Unix timestamps are traditionally stored as a signed 32-bit integer. The maximum value is 2,147,483,647, which corresponds to:
January 19, 2038, 03:14:07 UTC
After that, 32-bit systems will overflow and wrap to negative numbers (interpreted as 1901). This is the "Y2K38" problem.
64-bit systems (most modern computers) won't have this problem. They can handle dates until the year 292 billion.
Converting
// Current timestamp
const now = Math.floor(Date.now() / 1000);
// Timestamp to Date
const date = new Date(1706745600 * 1000);
// Date to timestamp
const timestamp = Math.floor(new Date('2024-01-31').getTime() / 1000);
// Readable format
new Date(1706745600 * 1000).toISOString();
// "2024-01-31T16:00:00.000Z"
Common Operations
// Add 1 hour
const future = timestamp + 3600;
// Subtract 1 day
const yesterday = timestamp - 86400;
// Check if expired
const isExpired = Date.now() / 1000 > expirationTimestamp;
// Time until expiration
const secondsLeft = expirationTimestamp - Date.now() / 1000;
Common Gotchas
- Seconds vs milliseconds - JavaScript uses ms, Unix uses seconds. Don't forget to convert.
- Timezone confusion - Timestamps are always UTC. Convert to local time for display only.
- Leap seconds - Unix time ignores them. 86,400 seconds is always one day.
- Negative timestamps - Dates before 1970 are negative numbers.
Unix Time vs ISO 8601
| Unix Timestamp | ISO 8601 | |
|---|---|---|
| Format | 1706745600 | 2024-01-31T16:00:00Z |
| Human readable | No | Yes |
| Size | 10 bytes | 20+ bytes |
| Timezone | Always UTC | Can include offset |
| Best for | Storage, computation | Display, interchange |
Try It
Convert Timestamp"Time is an illusion. Unix time doubly so." — with apologies to Douglas Adams