Converting Epoch to Human Date
How to convert a Unix epoch timestamp to a human-readable date in JavaScript, Python, Java, Go, SQL, and the command line. Quick reference with examples.
Conversion
1704067200 → 2024-01-01
Detailed Explanation
Converting a Unix epoch timestamp to a human-readable date is one of the most frequent operations in programming. The process involves taking the number of seconds (or milliseconds) since January 1, 1970 UTC and translating it into a year, month, day, hour, minute, and second representation.
Conversions in every major language:
// JavaScript (input: milliseconds)
new Date(1704067200 * 1000).toISOString()
// "2024-01-01T00:00:00.000Z"
new Date(1704067200 * 1000).toLocaleString("en-US", {
timeZone: "America/New_York"
})
// "12/31/2023, 7:00:00 PM"
# Python
from datetime import datetime, timezone
datetime.fromtimestamp(1704067200, tz=timezone.utc)
# datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc)
// Java
Instant.ofEpochSecond(1704067200L)
.atZone(ZoneId.of("UTC"))
.format(DateTimeFormatter.ISO_DATE_TIME)
// "2024-01-01T00:00:00Z"
// Go
time.Unix(1704067200, 0).UTC().Format(time.RFC3339)
// "2024-01-01T00:00:00Z"
-- MySQL
SELECT FROM_UNIXTIME(1704067200); -- '2024-01-01 00:00:00'
-- PostgreSQL
SELECT to_timestamp(1704067200); -- '2024-01-01 00:00:00+00'
# Linux/macOS
date -d @1704067200 # Linux
date -r 1704067200 # macOS
Key considerations:
Always know whether your input timestamp is in seconds or milliseconds. JavaScript's Date constructor expects milliseconds, so you must multiply seconds by 1000. Getting this wrong produces a date in January 1970 instead of the expected date.
Timezone matters on output: The same timestamp produces different readable dates depending on the timezone you apply. 1704067200 is midnight January 1, 2024 in UTC, but it is 7:00 PM on December 31, 2023 in New York. Always specify the desired timezone explicitly in your formatting call.
Batch conversions: When converting many timestamps (e.g., processing log files), create the formatter or timezone object once outside the loop rather than on every iteration. This can be 10-100x faster depending on the language.
Use Case
Log analysis tools must convert epoch timestamps from server logs into human-readable dates in the analyst's local timezone to make debugging production issues practical.