Epoch / Unix Timestamp Format Guide
Learn about Unix epoch timestamps: seconds and milliseconds since January 1, 1970. Covers conversions, Y2K38 problem, and usage across programming languages.
Detailed Explanation
Unix Epoch Timestamps
A Unix timestamp (also called epoch time or POSIX time) represents a point in time as the number of seconds that have elapsed since January 1, 1970, 00:00:00 UTC — known as the Unix epoch.
Seconds vs Milliseconds
Two common variants exist:
1772236800 # Seconds (10 digits) — traditional Unix
1772236800000 # Milliseconds (13 digits) — JavaScript, Java
When you encounter a large integer representing time, the digit count tells you the precision:
- 10 digits — seconds (standard Unix)
- 13 digits — milliseconds (JavaScript
Date.now(), JavaSystem.currentTimeMillis()) - 16 digits — microseconds (Python
time.time_ns() // 1000) - 19 digits — nanoseconds (Go
time.Now().UnixNano())
Advantages
- Timezone-neutral — A timestamp represents an absolute moment regardless of timezone
- Compact — A single integer is smaller than any formatted string
- Sortable and comparable — Simple numeric comparison
- Language-agnostic — Every language can work with integers
The Y2K38 Problem
Timestamps stored as 32-bit signed integers overflow on January 19, 2038 at 03:14:07 UTC (2,147,483,647 seconds after epoch). This is analogous to Y2K but for Unix systems. Modern systems use 64-bit integers, which won't overflow for approximately 292 billion years.
Conversion Across Languages
| Language | To timestamp | From timestamp |
|---|---|---|
| JavaScript | Math.floor(Date.now() / 1000) |
new Date(ts * 1000) |
| Python | int(time.time()) |
datetime.fromtimestamp(ts) |
| Java | Instant.now().getEpochSecond() |
Instant.ofEpochSecond(ts) |
| PHP | time() |
date('Y-m-d', $ts) |
| Go | time.Now().Unix() |
time.Unix(ts, 0) |
| Ruby | Time.now.to_i |
Time.at(ts) |
| C# | DateTimeOffset.UtcNow.ToUnixTimeSeconds() |
DateTimeOffset.FromUnixTimeSeconds(ts) |
Negative Timestamps
Dates before the epoch (pre-1970) are represented as negative numbers. For example, January 1, 1969 00:00:00 UTC is -31536000.
Use Case
Unix timestamps are the standard time representation in databases (PostgreSQL, MySQL), message queues (Kafka offsets), JWT token expiration claims (exp, iat, nbf), API rate limiting, cache expiration headers, and log aggregation systems. They are ideal when you need timezone-independent time comparison.