Unix Timestamps: Format, Units, Timezones
What a Unix timestamp stores: seconds vs milliseconds, how to read and format one, and the timezone mistakes that shift a date by hours.
Need to convert a value right now? The Unix Timestamp Converter runs in your browser. This article explains what the number means: seconds vs milliseconds, how to read and format one, and where timezone bugs come from.
What is a Unix Timestamp?
A Unix timestamp is the number of seconds elapsed since January 1, 1970, 00:00:00 UTC (known as the Unix Epoch). This seemingly simple number is the foundation of unified time representation across the entire computing world.
Unix Epoch: 1970-01-01T00:00:00Z
Current: 1730764800 (approximately 2024-11-05)
Why 1970? This was a technical decision from the early Unix operating system era, and it became the industry standard. Nearly every programming language and database natively supports Unix timestamps.
Seconds vs Milliseconds
Unix timestamps come in two common precisions:
| Type | Digits | Example | Source |
|---|---|---|---|
| Seconds | 10 | 1730764800 |
Unix time(), Go time.Now().Unix() |
| Milliseconds | 13 | 1730764800000 |
JavaScript Date.now(), Java System.currentTimeMillis() |
The most common mistake is confusing these two precisions. Treating a millisecond timestamp as seconds results in dates in the year 56789; the reverse loses precision.
Quick Detection
function isMilliseconds(ts) {
return ts.toString().length === 13;
}
// Or more robust approach
function normalizeTimestamp(ts) {
return ts > 1e12 ? ts : ts * 1000; // Auto-convert to milliseconds
}
Timezones: The Biggest Pitfall
Unix timestamps themselves have no timezone concept — they represent an absolute moment in time. But when you convert a timestamp to a human-readable date, timezones become critical.
Example
Timestamp: 1730764800
UTC: 2024-11-05 00:00:00
Beijing: 2024-11-05 08:00:00 (UTC+8)
New York: 2024-11-04 19:00:00 (UTC-5, EST)
London: 2024-11-05 00:00:00 (UTC+0, GMT)
Tokyo: 2024-11-05 09:00:00 (UTC+9)
The same timestamp shows different dates and times in different timezones, but represents the same moment.
Common Mistakes
Mistake 1: Formatting without considering timezone
// Dangerous: uses local timezone, different users see different results
new Date(1730764800 * 1000).toLocaleString()
// Chinese user: "2024/11/5 08:00:00"
// US user: "11/4/2024, 7:00:00 PM"
Correct approach: Explicitly specify timezone
// Always show UTC
new Date(1730764800 * 1000).toISOString()
// "2024-11-05T00:00:00.000Z"
// Or specify timezone
new Date(1730764800 * 1000).toLocaleString('en-US', {
timeZone: 'America/New_York'
})
// "11/4/2024, 7:00:00 PM"
Mistake 2: Mixing timezones in databases
Always store timestamps in UTC. Only convert to local timezone when displaying to users. This avoids daylight saving time and timezone change issues.
Timestamp Handling Across Languages
JavaScript / TypeScript
// Get current timestamp (milliseconds)
Date.now() // 1730764800000
new Date().getTime() // 1730764800000
// Timestamp → Date
new Date(1730764800 * 1000)
// Date → Timestamp (seconds)
Math.floor(Date.now() / 1000)
// ISO string → Timestamp
Date.parse("2024-11-05T00:00:00Z") / 1000
Python
import time
from datetime import datetime, timezone
# Get current timestamp (seconds)
time.time() # 1730764800.123
int(time.time()) # 1730764800
# Timestamp → Date (UTC)
datetime.fromtimestamp(1730764800, tz=timezone.utc)
# Date → Timestamp
int(datetime(2024, 11, 5, tzinfo=timezone.utc).timestamp())
Go
import "time"
// Get current timestamp (seconds)
time.Now().Unix() // 1730764800
time.Now().UnixMilli() // 1730764800000
// Timestamp → Time
time.Unix(1730764800, 0)
// Time → Timestamp
time.Date(2024, 11, 5, 0, 0, 0, 0, time.UTC).Unix()
Java
// Get current timestamp (milliseconds)
System.currentTimeMillis() // 1730764800000
// Timestamp → Date
Instant.ofEpochSecond(1730764800)
// Date → Timestamp (seconds)
Instant.now().getEpochSecond()
Practical Scenarios
1. Calculating Time Differences
const start = Date.now();
// ... perform some operation ...
const elapsed = Date.now() - start; // milliseconds
console.log(`Elapsed ${elapsed}ms`);
2. Cache Expiration Check
const CACHE_TTL = 3600; // 1 hour (seconds)
const cached = { data: "...", timestamp: 1730764800 };
if (Date.now() / 1000 - cached.timestamp < CACHE_TTL) {
// Cache not expired
}
3. Generating Time-based Unique IDs
// Snowflake ID timestamp part (milliseconds)
const timestampPart = Date.now();
Online Tools
These tools significantly boost efficiency when working with timestamps:
- Use the Unix Timestamp Converter to quickly convert between second/millisecond timestamps and dates, viewing UTC, local time, and ISO 8601 formats simultaneously
- The live timestamp at the top of the page can be copied with one click
Understanding the essence of Unix timestamps — an absolute moment without timezone — is the foundation for correctly handling date and time problems.