Last updated
Unix Timestamp Converter — Examples
Unix timestamps represent time as seconds (or milliseconds) since January 1, 1970 UTC. Here are practical examples for all common timestamp operations.
Get the Current Unix Timestamp
// JavaScript — current Unix timestamp
const nowSeconds = Math.floor(Date.now() / 1000);
const nowMs = Date.now();
console.log("Seconds:", nowSeconds); // e.g., 1710000000
console.log("Milliseconds:", nowMs); // e.g., 1710000000000
# Python — current Unix timestamp
import time
from datetime import datetime, timezone
now_seconds = int(time.time())
now_ms = int(time.time() * 1000)
print(f"Seconds: {now_seconds}")
print(f"Milliseconds: {now_ms}")
Timestamp to Date
// JavaScript — Unix timestamp to date
function timestampToDate(ts) {
// Auto-detect seconds vs milliseconds
const ms = ts > 1e10 ? ts : ts * 1000;
const date = new Date(ms);
return {
utc: date.toUTCString(),
iso8601: date.toISOString(),
local: date.toLocaleString()
};
}
console.log(timestampToDate(1710000000)); // seconds
console.log(timestampToDate(1710000000000)); // milliseconds
# Python — Unix timestamp to date
from datetime import datetime, timezone
def timestamp_to_date(ts):
# Auto-detect seconds vs milliseconds
if ts > 1e10:
ts = ts / 1000
dt = datetime.fromtimestamp(ts, tz=timezone.utc)
return {
"utc": dt.strftime("%Y-%m-%d %H:%M:%S UTC"),
"iso8601": dt.isoformat(),
"date": dt.strftime("%B %d, %Y")
}
print(timestamp_to_date(1710000000))
Date to Timestamp
// JavaScript — date to Unix timestamp
function dateToTimestamp(dateStr) {
const date = new Date(dateStr);
return {
seconds: Math.floor(date.getTime() / 1000),
milliseconds: date.getTime()
};
}
console.log(dateToTimestamp("2024-03-09T16:00:00Z"));
console.log(dateToTimestamp("2024-01-01"));
# Python — date to Unix timestamp
from datetime import datetime, timezone
def date_to_timestamp(date_str):
dt = datetime.fromisoformat(date_str.replace("Z", "+00:00"))
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return {
"seconds": int(dt.timestamp()),
"milliseconds": int(dt.timestamp() * 1000)
}
print(date_to_timestamp("2024-03-09T16:00:00Z"))
Timestamp Arithmetic
// JavaScript — add/subtract time from a timestamp
const now = Math.floor(Date.now() / 1000);
const oneHourAgo = now - 3600;
const oneDayAgo = now - 86400;
const oneWeekAgo = now - 604800;
const oneMonthAhead = now + 2592000;
console.log("1 hour ago:", new Date(oneHourAgo * 1000).toISOString());
console.log("1 day ago:", new Date(oneDayAgo * 1000).toISOString());
console.log("1 week ago:", new Date(oneWeekAgo * 1000).toISOString());
# Python — timestamp arithmetic
from datetime import datetime, timezone, timedelta
now = datetime.now(timezone.utc)
one_hour_ago = now - timedelta(hours=1)
one_day_ago = now - timedelta(days=1)
one_week_ago = now - timedelta(weeks=1)
next_month = now + timedelta(days=30)
print(f"1 hour ago: {int(one_hour_ago.timestamp())}")
print(f"1 day ago: {int(one_day_ago.timestamp())}")
print(f"Next month: {int(next_month.timestamp())}")
Timestamp Difference
// JavaScript — calculate time between two timestamps
function timeDifference(ts1, ts2) {
const diffSeconds = Math.abs(ts2 - ts1);
return {
seconds: diffSeconds,
minutes: Math.floor(diffSeconds / 60),
hours: Math.floor(diffSeconds / 3600),
days: Math.floor(diffSeconds / 86400)
};
}
const start = 1700000000;
const end = 1710000000;
console.log(timeDifference(start, end));
Database Timestamp Queries
-- MySQL — work with Unix timestamps
SELECT FROM_UNIXTIME(1710000000) AS date_utc;
SELECT UNIX_TIMESTAMP('2024-03-09 16:00:00') AS unix_ts;
-- Find records from the last 24 hours
SELECT * FROM events
WHERE created_at > UNIX_TIMESTAMP() - 86400;
-- PostgreSQL
SELECT TO_TIMESTAMP(1710000000) AT TIME ZONE 'UTC';
SELECT EXTRACT(EPOCH FROM NOW())::BIGINT;
-- SQLite
SELECT DATETIME(1710000000, 'unixepoch');
SELECT STRFTIME('%s', 'now');
Year 2038 Problem Reference
// The Year 2038 problem: 32-bit signed integers overflow
const MAX_INT32 = 2147483647; // 2^31 - 1
const overflowDate = new Date(MAX_INT32 * 1000);
console.log("32-bit overflow:", overflowDate.toISOString());
// 2038-01-19T03:14:07.000Z
// Solution: use 64-bit integers (JavaScript numbers are 64-bit floats)
// Most modern systems already use 64-bit timestamps
const farFuture = 9999999999; // Year 2286
console.log("Far future:", new Date(farFuture * 1000).toISOString());
Common Reference Timestamps
- Unix epoch:
0→ January 1, 1970 00:00:00 UTC - Y2K:
946684800→ January 1, 2000 00:00:00 UTC - Year 2038 problem:
2147483647→ January 19, 2038 03:14:07 UTC - Current (approx):
1710000000→ March 2024
Unix timestamps are the universal time format in computing. They're simple to store, compare, and calculate with — and every language and database supports them natively.