Unix timestamps are the foundation of time handling in computing. Simple yet powerful, understanding them is essential for any developer working with dates and times.
What is a Unix Timestamp?
A Unix timestamp (also called Epoch time or POSIX time) is the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC—the Unix Epoch.
Unix Timestamp: 1705312800
Represents: January 15, 2024, at 10:00:00 UTC
That's it—just a number counting seconds. No timezones, no date formats, no ambiguity.
The Unix Epoch
The Epoch is the reference point:
January 1, 1970, 00:00:00 UTC = 0
Why 1970? It was chosen when Unix was being developed in the early 1970s. A recent date meant smaller numbers, and it was already in the past (no negative values for current times).
Some notable timestamps:
| Timestamp | Date | Event |
|---|---|---|
| 0 | Jan 1, 1970 | Unix Epoch |
| 1000000000 | Sep 9, 2001 | "Unix Billennium" |
| 1234567890 | Feb 13, 2009 | Memorable number |
| 2000000000 | May 18, 2033 | Two billion |
| 2147483647 | Jan 19, 2038 | Y2K38 problem |
Seconds vs Milliseconds
Unix timestamps can be in seconds or milliseconds:
// Seconds (10 digits for current dates)
1705312800
// Milliseconds (13 digits for current dates)
1705312800000
How to tell them apart:
- 10 digits: seconds
- 13 digits: milliseconds
JavaScript uses milliseconds; most other languages use seconds.
// JavaScript
Date.now() // 1705312800000 (milliseconds)
// Python
import time
time.time() # 1705312800.123 (seconds with decimals)
Why Timestamps Are Useful
1. Language/System Agnostic
Every language can work with integers:
// JavaScript
new Date(1705312800 * 1000)
// Python
datetime.fromtimestamp(1705312800)
// SQL
FROM_UNIXTIME(1705312800)
2. Easy Date Math
Adding/subtracting time is just arithmetic:
const now = 1705312800;
const oneHourLater = now + 3600; // Add 3600 seconds
const oneDayAgo = now - 86400; // Subtract 86400 seconds
const oneWeekLater = now + 604800; // 7 * 24 * 60 * 60
3. Easy Comparison
if (timestamp1 < timestamp2) {
// timestamp1 is earlier
}
4. Sortable
Timestamps sort chronologically automatically.
5. Storage Efficient
An integer takes less space than a date string:
- Timestamp: 4 bytes (32-bit) or 8 bytes (64-bit)
- ISO 8601 string: 20+ bytes
Converting Timestamps to Dates
JavaScript
// Timestamp to Date
const date = new Date(1705312800 * 1000); // Note: multiply by 1000
console.log(date.toISOString()); // "2024-01-15T10:00:00.000Z"
// Date to timestamp
const timestamp = Math.floor(Date.now() / 1000); // Divide by 1000 for seconds
Python
from datetime import datetime
# Timestamp to Date
date = datetime.fromtimestamp(1705312800) # Local time
date_utc = datetime.utcfromtimestamp(1705312800) # UTC
# Date to timestamp
timestamp = datetime.now().timestamp()
Unix/Linux Command Line
# Current timestamp
date +%s
# Timestamp to date
date -d @1705312800
# Date to timestamp
date -d "2024-01-15 10:00:00" +%s
Timezone Handling
Unix timestamps are always UTC. The number 1705312800 means the same instant worldwide.
When displaying to users, convert to their local time:
const timestamp = 1705312800;
const date = new Date(timestamp * 1000);
// Local time (depends on user's timezone)
date.toString(); // "Mon Jan 15 2024 05:00:00 GMT-0500 (EST)"
// UTC
date.toUTCString(); // "Mon, 15 Jan 2024 10:00:00 GMT"
Best Practices
- Store in UTC: Always store timestamps (they're inherently UTC)
- Convert on display: Show times in user's local timezone
- Accept with timezone: When receiving times, require timezone or assume UTC
# Storing
created_at = int(time.time()) # UTC timestamp
# Displaying
local_time = datetime.fromtimestamp(created_at, tz=user_timezone)
The Y2K38 Problem
32-bit signed integers can store up to 2,147,483,647.
Maximum 32-bit timestamp: 2147483647
Represents: January 19, 2038, at 03:14:07 UTC
After this moment, 32-bit timestamps overflow and wrap to negative (representing 1901).
Who's Affected?
- Embedded systems
- Legacy 32-bit software
- Databases with 32-bit timestamp columns
- Old programming language implementations
Solutions
- Use 64-bit timestamps (most modern systems)
- Use dedicated datetime types
- Plan for upgrades before 2038
// Old (32-bit problem)
time_t timestamp; // May be 32-bit
// New (64-bit safe)
int64_t timestamp; // Definitely 64-bit
64-bit timestamps last until the year 292 billion—plenty of runway!
Timestamps in Different Contexts
JavaScript
// Current time in milliseconds
Date.now() // 1705312800000
// Current time in seconds
Math.floor(Date.now() / 1000) // 1705312800
// Specific date to timestamp
new Date('2024-01-15T10:00:00Z').getTime() / 1000
// Timestamp to date
new Date(timestamp * 1000)
Python
import time
from datetime import datetime
# Current timestamp
time.time() # 1705312800.123456
# Integer timestamp
int(time.time()) # 1705312800
# Specific date to timestamp
datetime(2024, 1, 15, 10, 0, 0).timestamp()
# Timestamp to datetime
datetime.fromtimestamp(1705312800)
SQL
-- MySQL
SELECT UNIX_TIMESTAMP(); -- Current
SELECT UNIX_TIMESTAMP('2024-01-15 10:00:00'); -- Specific date
SELECT FROM_UNIXTIME(1705312800); -- To datetime
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM NOW()); -- Current
SELECT EXTRACT(EPOCH FROM '2024-01-15 10:00:00'::timestamp);
SELECT TO_TIMESTAMP(1705312800);
Common Timestamp Operations
Time Since Event
const eventTime = 1705312800;
const now = Math.floor(Date.now() / 1000);
const secondsAgo = now - eventTime;
const minutesAgo = Math.floor(secondsAgo / 60);
const hoursAgo = Math.floor(minutesAgo / 60);
const daysAgo = Math.floor(hoursAgo / 24);
Is Timestamp in Past/Future?
const isInPast = timestamp < Date.now() / 1000;
const isInFuture = timestamp > Date.now() / 1000;
Add Duration
const oneHour = 3600;
const oneDay = 86400;
const oneWeek = 604800;
const futureTime = now + oneDay;
Start/End of Day
// Start of today (UTC)
const startOfDay = Math.floor(Date.now() / 86400000) * 86400;
// End of today (UTC)
const endOfDay = startOfDay + 86399;
ISO 8601 vs Unix Timestamps
Both are valid ways to represent time:
| Format | Example | Use Case |
|---|---|---|
| Unix | 1705312800 |
Storage, calculations, APIs |
| ISO 8601 | 2024-01-15T10:00:00Z |
Human readability, logs, display |
Many APIs accept both:
{
"created_at": 1705312800,
"created_at_iso": "2024-01-15T10:00:00Z"
}
Summary
Unix timestamps are simple yet powerful:
- What: Seconds since January 1, 1970 UTC
- Why: Language-agnostic, easy math, sortable, compact
- Watch out: Seconds vs milliseconds, 32-bit Y2K38
- Best practice: Store as UTC timestamps, display in local time
They're the universal language of time in computing—learn them once, use them everywhere.
Need to convert timestamps? Try our Unix Timestamp Converter!