unix timestamptimejavascriptpython
How to Read and Convert Unix Timestamps
Understand Unix timestamps - what they are, how to convert them, and how to work with them in different programming languages.
May 1, 2024ยท5 min read
What is a Unix Timestamp?
A Unix timestamp is the number of seconds elapsed since January 1, 1970 at 00:00:00 UTC (the "Unix Epoch"). It's the universal standard for representing moments in time in computing.
Current timestamp: approximately 1,700,000,000 (as of late 2023)
Converting in Different Languages
JavaScript
// Current timestamp
const now = Math.floor(Date.now() / 1000)
// Timestamp to Date
const date = new Date(1700000000 * 1000)
console.log(date.toISOString()) // "2023-11-14T22:13:20.000Z"
// Date to timestamp
const ts = Math.floor(new Date('2024-01-01').getTime() / 1000)
Python
import datetime, time
# Current timestamp
now = int(time.time())
# Timestamp to datetime
dt = datetime.datetime.fromtimestamp(1700000000)
# Datetime to timestamp
ts = int(datetime.datetime(2024, 1, 1).timestamp())
SQL
-- MySQL: timestamp to datetime
SELECT FROM_UNIXTIME(1700000000);
-- PostgreSQL
SELECT to_timestamp(1700000000);
Common Gotchas
- Milliseconds vs seconds: JavaScript's
Date.now()returns milliseconds; divide by 1000 for Unix timestamp - Timezone: Unix timestamps are always UTC; conversion to local time is done at display time
- Year 2038 problem: 32-bit signed integers overflow on Jan 19, 2038. Use 64-bit integers.
Convert timestamps instantly with our Unix Timestamp Converter.