A Unix timestamp is a single integer representing seconds since January 1, 1970 UTC. Every programming language and database supports converting them to human-readable dates — but the tricky part is seconds vs milliseconds, timezones, and output formatting. This guide covers it all.
What Is a Unix Timestamp?
The Unix epoch is January 1, 1970, 00:00:00 UTC. A Unix timestamp counts seconds elapsed since that moment. It is timezone-independent — the same integer represents the same moment in time everywhere in the world.
0 → 1970-01-01 00:00:00 UTC (the epoch itself)
1000000000 → 2001-09-09 01:46:40 UTC
1741737600 → 2025-03-12 00:00:00 UTC
2147483647 → 2038-01-19 03:14:07 UTC (Year 2038 problem — 32-bit max)
4294967295 → 2106-02-07 06:28:15 UTC (32-bit unsigned max)
Seconds vs Milliseconds — The 10/13 Digit Rule
This is the most common source of confusion. JavaScript's Date.now() returns milliseconds. Most Unix APIs return seconds.
| Value | Digits | Unit | Date |
|---|
| 1741737600 | 10 digits | Seconds | 2025-03-12 |
| 1741737600000 | 13 digits | Milliseconds | 2025-03-12 |
| 1741737600000000 | 16 digits | Microseconds | 2025-03-12 |
Converting in JavaScript
// Unix timestamp in seconds → Date
const ts = 1741737600;
const date = new Date(ts * 1000); // JavaScript Date takes milliseconds
// Formatting options
date.toISOString(); // '2025-03-12T00:00:00.000Z'
date.toLocaleDateString(); // '3/12/2025' (locale-dependent)
date.toLocaleString('en-US', {
timeZone: 'America/New_York',
dateStyle: 'full',
timeStyle: 'short',
}); // 'Tuesday, March 11, 2025 at 8:00 PM'
// Intl.DateTimeFormat for consistent formatting
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZone: 'UTC',
});
formatter.format(date); // 'Mar 12, 2025, 12:00 AM'
// Get current timestamp in seconds
const nowSecs = Math.floor(Date.now() / 1000);
// Auto-detect seconds vs milliseconds
function toDate(ts) {
// If more than 12 digits, treat as milliseconds
return ts > 1e12 ? new Date(ts) : new Date(ts * 1000);
}
Converting in Python
from datetime import datetime, timezone
ts = 1741737600
# Seconds → datetime (UTC)
dt_utc = datetime.fromtimestamp(ts, tz=timezone.utc)
print(dt_utc) # 2025-03-12 00:00:00+00:00
print(dt_utc.isoformat()) # '2025-03-12T00:00:00+00:00'
# Local timezone
dt_local = datetime.fromtimestamp(ts)
print(dt_local) # Local time
# Formatting
dt_utc.strftime('%Y-%m-%d %H:%M:%S') # '2025-03-12 00:00:00'
dt_utc.strftime('%B %d, %Y') # 'March 12, 2025'
# Current timestamp in seconds
import time
now_secs = int(time.time())
# With timezone support (Python 3.9+)
from zoneinfo import ZoneInfo
dt_ny = datetime.fromtimestamp(ts, tz=ZoneInfo('America/New_York'))
print(dt_ny) # 2025-03-11 20:00:00-04:00
PostgreSQL and MySQL Timestamp Functions
-- PostgreSQL
-- Convert Unix seconds to timestamp
SELECT TO_TIMESTAMP(1741737600);
-- → 2025-03-12 00:00:00+00
-- Convert timestamp to Unix seconds
SELECT EXTRACT(EPOCH FROM NOW())::BIGINT;
-- → 1741737600
-- Format a timestamp
SELECT TO_CHAR(TO_TIMESTAMP(1741737600), 'YYYY-MM-DD HH24:MI:SS');
-- → '2025-03-12 00:00:00'
-- MySQL / MariaDB
-- Convert Unix seconds to datetime
SELECT FROM_UNIXTIME(1741737600);
-- → 2025-03-12 00:00:00
-- Convert datetime to Unix seconds
SELECT UNIX_TIMESTAMP(NOW());
-- → 1741737600
-- Format with FROM_UNIXTIME
SELECT FROM_UNIXTIME(1741737600, '%Y-%m-%d %H:%i:%s');
-- → '2025-03-12 00:00:00'
ISO 8601 Format
ISO 8601 is the international standard for date/time strings. Use it for API responses, logs, and data interchange — it's unambiguous and sortable lexicographically.
2026-03-14T09:00:00Z ← UTC (Z = zero offset)
2026-03-14T09:00:00.000Z ← UTC with milliseconds
2026-03-14T04:00:00-05:00 ← UTC-5 (Eastern Standard Time)
2026-03-14T14:30:00+05:30 ← UTC+5:30 (India Standard Time)
// JavaScript
new Date().toISOString()
// → '2026-03-14T09:00:00.000Z'
// Always use UTC for storage; format in local time for display
FAQ
What is a Unix timestamp?
A Unix timestamp (also called epoch time) is the number of seconds that have elapsed since January 1, 1970 00:00:00 UTC (the Unix epoch). It is a timezone-independent way to represent a point in time as a single integer. JavaScript's Date.now() returns milliseconds since epoch, not seconds.
How do I know if a timestamp is in seconds or milliseconds?
Count the digits. A 10-digit number is seconds (covers dates from 2001 to 2286). A 13-digit number is milliseconds (covers dates from 2001 to 2286 when divided by 1000). A quick rule: if the number is greater than 10^12, it's likely milliseconds.
How do I get the current Unix timestamp in JavaScript?
Use Math.floor(Date.now() / 1000) for seconds, or Date.now() for milliseconds. You can also use new Date().getTime() / 1000, or the newer performance.timeOrigin for high-resolution timing.
What is ISO 8601 format?
ISO 8601 is the international standard for representing dates and times. The format is YYYY-MM-DDTHH:mm:ss.sssZ, where Z means UTC. For example: 2026-03-14T09:00:00.000Z. JavaScript's Date.toISOString() outputs this format.
Why does my timestamp show the wrong date?
The most common cause is seconds vs milliseconds confusion. If new Date(timestamp) shows 1970 or a date in 1970, the timestamp is in seconds — multiply by 1000. If the date is 50+ years in the future, the timestamp might be in microseconds — divide by 1000.