In Unix and most programming languages, time is measured from January 1, 1970, 00:00:00 UTC:
Date.now() // 1705334400000 (milliseconds since epoch)
Math.floor(Date.now() / 1000) // 1705334400 (seconds)
This number represents time elapsed since the epoch.
What Is the Unix Epoch?
The Unix epoch is the starting point for Unix time—a system for describing points in time. It's defined as midnight UTC on January 1, 1970.
Every moment since then is represented as seconds (or milliseconds) elapsed.
Why January 1, 1970?
The date was chosen when Unix was being developed at Bell Labs in the early 1970s. The developers needed a starting point for their time system and picked a recent, round date: the start of the decade.
It's arbitrary but became standard as Unix spread.
How It Works
Unix time counts seconds since the epoch:
0 = January 1, 1970 00:00:00 UTC
86400 = January 2, 1970 00:00:00 UTC (1 day later)
1000000000 = September 9, 2001 01:46:40 UTC
Each day adds 86,400 seconds (60 × 60 × 24).
JavaScript and the Epoch
JavaScript uses milliseconds since epoch:
new Date(0) // Thu Jan 01 1970 00:00:00 GMT
new Date(1000) // Thu Jan 01 1970 00:00:01 GMT (1 second later)
Get current timestamp:
Date.now() // Current time in milliseconds
new Date().getTime() // Same thing
Other Languages
Python (seconds):
import time
time.time() # 1705334400.123
PHP (seconds):
time() // 1705334400
Ruby (seconds with decimals):
Time.now.to_f // 1705334400.123
Converting Timestamps
Timestamp to date:
const timestamp = 1705334400000;
const date = new Date(timestamp);
console.log(date); // Mon Jan 15 2024 ...
Date to timestamp:
const date = new Date('2024-01-15');
const timestamp = date.getTime();
Dates Before 1970
Negative timestamps represent dates before the epoch:
new Date(-86400000) // December 31, 1969
new Date(-31536000000) // January 1, 1969
The Year 2038 Problem
32-bit systems store Unix time as a signed 32-bit integer. This maxes out on January 19, 2038 at 03:14:07 UTC:
2^31 - 1 = 2,147,483,647 seconds
After this, the counter overflows and wraps to negative, jumping back to 1901.
64-bit systems don't have this problem—they're good for 292 billion years.
Leap Seconds
Unix time ignores leap seconds. It assumes every day is exactly 86,400 seconds, even when leap seconds are added to clocks.
This means Unix time can be off by a few seconds from UTC.
Common Operations
Time difference:
const start = Date.now();
// ... some operation
const end = Date.now();
const elapsed = end - start; // Milliseconds elapsed
Comparing dates:
const date1 = new Date('2024-01-15');
const date2 = new Date('2024-01-20');
if (date1.getTime() < date2.getTime()) {
console.log('date1 is earlier');
}
Adding time:
const now = Date.now();
const oneHourLater = now + (60 * 60 * 1000);
const oneDayLater = now + (24 * 60 * 60 * 1000);
Storage
Timestamps are compact for storage:
// JSON
{ "createdAt": 1705334400 }
// vs
{ "createdAt": "2024-01-15T12:00:00Z" }
Numbers are smaller and easier to compare.
Timezone Independence
Unix timestamps are timezone-agnostic. They represent absolute points in time:
const timestamp = 1705334400000;
// Same moment everywhere
new Date(timestamp) // Displays in local timezone
Database Usage
Many databases store timestamps as integers:
CREATE TABLE events (
id INT,
timestamp BIGINT
);
INSERT INTO events VALUES (1, 1705334400);
API Responses
APIs often return timestamps:
{
"id": 123,
"message": "Hello",
"timestamp": 1705334400,
"createdAt": "2024-01-15T12:00:00Z"
}
Some use Unix time, others use ISO 8601 strings.
Further Reading
Wikipedia's Unix time article covers the format comprehensively.
The Unix timestamp converter helps visualize conversions.
MDN's Date documentation covers JavaScript's time handling.
The Unix epoch is foundational to how computers track time.
0 comments