Unix Timestamp

Time as a single number—seconds since 1970. Simple, universal, timezone-free.

3 min read

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

FormatExampleNotes
Seconds1706745600Standard Unix timestamp
Milliseconds1706745600000JavaScript, Java
Microseconds1706745600000000High-precision systems
Nanoseconds1706745600000000000Go, some databases
⚠️Watch the Zeros

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

BenefitWhy It Matters
Timezone-neutralStore once, display in any timezone
Compact10 digits vs "2024-01-31T12:00:00.000Z"
SortableJust compare numbers
Math-friendlyAdd/subtract seconds easily
UniversalEvery 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.

ℹ️Modern Systems Are Fine

64-bit systems (most modern computers) won't have this problem. They can handle dates until the year 292 billion.

Converting

javascript
// 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

javascript
// 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 TimestampISO 8601
Format17067456002024-01-31T16:00:00Z
Human readableNoYes
Size10 bytes20+ bytes
TimezoneAlways UTCCan include offset
Best forStorage, computationDisplay, interchange

Try It

Convert Timestamp

"Time is an illusion. Unix time doubly so." — with apologies to Douglas Adams