Unix Timestamps Explained
Understanding Epoch Time
Learn About TimestampsA Unix timestamp like "1704067200" means January 1, 2024, at midnight UTC. This simple number—seconds since January 1, 1970—is how computers universally represent time. Understanding Unix timestamps is essential for programming, databases, and working with APIs.
Why 1970?
The Unix operating system was developed in the late 1960s at Bell Labs. When designers needed an arbitrary starting point for timekeeping:
- January 1, 1970 was chosen as a round number near the system's creation
- It allowed 32-bit integers to cover dates well into the future
- Starting at midnight UTC kept it simple and universal
This "epoch" became the de facto standard across computing.
Converting Timestamps
Timestamp to Date
| Timestamp | Human-Readable (UTC) |
|---|---|
| 0 | January 1, 1970 00:00:00 |
| 86400 | January 2, 1970 00:00:00 |
| 1000000000 | September 9, 2001 01:46:40 |
| 1234567890 | February 13, 2009 23:31:30 |
| 1704067200 | January 1, 2024 00:00:00 |
| 2000000000 | May 18, 2033 03:33:20 |
| 2147483647 | January 19, 2038 03:14:07 (32-bit limit) |
Useful Time Intervals
| Period | Seconds |
|---|---|
| 1 minute | 60 |
| 1 hour | 3,600 |
| 1 day | 86,400 |
| 1 week | 604,800 |
| 1 year (approx) | 31,536,000 |
Millisecond Timestamps
Many systems use milliseconds instead of seconds:
| Format | Example (same moment) | Used By |
|---|---|---|
| Seconds | 1704067200 | Unix, PHP, Python |
| Milliseconds | 1704067200000 | JavaScript, Java, JSON APIs |
| Microseconds | 1704067200000000 | Some databases |
| Nanoseconds | 1704067200000000000 | Go, some systems |
To convert: Milliseconds ÷ 1000 = Seconds
Working with Timestamps in Code
JavaScript
// Current timestamp (milliseconds)
Date.now(); // 1704067200000
// Timestamp to date
new Date(1704067200 * 1000);
// Date to timestamp
Math.floor(new Date('2024-01-01').getTime() / 1000);Python
import time, datetime
# Current timestamp
time.time() # 1704067200.0
# Timestamp to datetime
datetime.datetime.fromtimestamp(1704067200)
# Datetime to timestamp
datetime.datetime(2024, 1, 1).timestamp()SQL
-- MySQL: Current timestamp
SELECT UNIX_TIMESTAMP();
-- Convert timestamp to date
SELECT FROM_UNIXTIME(1704067200);
-- Date to timestamp
SELECT UNIX_TIMESTAMP('2024-01-01');Advantages of Unix Timestamps
- Time zone neutral: Always UTC, no DST issues
- Easy arithmetic: Add/subtract seconds for time math
- Compact storage: Single integer vs. formatted string
- Sorting: Natural numeric sort equals chronological sort
- Universal: Works across languages and systems
When to Use Timestamps vs. Formatted Dates
- Timestamps: Storage, calculations, APIs, databases
- Formatted dates: User display, reports, logs
Conclusion
Unix timestamps count seconds since January 1, 1970 UTC. This simple system enables universal time representation across computers and programming languages. Remember that some systems use milliseconds (13 digits) instead of seconds (10 digits), and that 32-bit systems face a limit in 2038. For storage and calculations, timestamps are ideal; for display, convert to human-readable formats.