Code Guide

How to Convert Unix Timestamps in JavaScript, Python, and SQL

๐Ÿ“… September 2026โฑ๏ธ 5 min read
Every language and database handles Unix timestamps slightly differently โ€” different units, different function names. Here's the copy-paste snippet for each of the most common ones.

JavaScript

JavaScript's Date object expects milliseconds, so a standard (seconds-based) Unix timestamp needs to be multiplied by 1000 first:

// Seconds-based timestamp -> Date
const timestamp = 1735689600; // seconds
const date = new Date(timestamp * 1000);
console.log(date.toISOString()); // "2025-01-01T00:00:00.000Z"

// Current timestamp
const nowMs = Date.now();                    // milliseconds, e.g. 1767225600000
const nowSec = Math.floor(Date.now() / 1000); // seconds, e.g. 1767225600

// Date -> seconds-based timestamp
const backToSeconds = Math.floor(date.getTime() / 1000);

Python

Python's standard library represents Unix time in seconds (often as a float, for sub-second precision), so no unit conversion is needed for typical epoch integers:

from datetime import datetime, timezone

# Timestamp -> datetime (local time)
ts = 1735689600
dt = datetime.fromtimestamp(ts)
print(dt)  # 2025-01-01 00:00:00 (in local timezone)

# Timestamp -> datetime (explicit UTC)
dt_utc = datetime.fromtimestamp(ts, tz=timezone.utc)
print(dt_utc)  # 2025-01-01 00:00:00+00:00

# Current timestamp
now_ts = datetime.now().timestamp()   # float, e.g. 1767225600.482913
now_ts_int = int(datetime.now().timestamp())

# datetime -> timestamp
back_to_ts = dt.timestamp()

SQL

PostgreSQL

-- Timestamp (seconds) -> timestamptz
SELECT to_timestamp(1735689600);
-- Result: 2025-01-01 00:00:00+00

-- Current time -> Unix timestamp (seconds, with fractional part)
SELECT EXTRACT(EPOCH FROM now());

-- Current time -> Unix timestamp (integer seconds)
SELECT EXTRACT(EPOCH FROM now())::bigint;

MySQL

-- Timestamp (seconds) -> DATETIME
SELECT FROM_UNIXTIME(1735689600);
-- Result: 2025-01-01 00:00:00

-- Current time -> Unix timestamp
SELECT UNIX_TIMESTAMP();

-- Specific datetime -> Unix timestamp
SELECT UNIX_TIMESTAMP('2025-01-01 00:00:00');

SQL Server

SQL Server has no single built-in "from Unix timestamp" function โ€” the standard approach is to add the timestamp (in seconds) to the epoch date directly:

-- Timestamp (seconds) -> datetime
SELECT DATEADD(SECOND, 1735689600, '1970-01-01');
-- Result: 2025-01-01 00:00:00.000

-- datetime -> Unix timestamp (seconds)
SELECT DATEDIFF(SECOND, '1970-01-01', GETUTCDATE());
๐Ÿ’ก Remember the unit mismatch

The most common bug across all of these languages isn't the syntax โ€” it's forgetting that JavaScript works in milliseconds while Python and most SQL functions work in seconds. If a converted date lands in 1970 or thousands of years in the future, check whether you need to multiply or divide by 1000.

Convert a Timestamp Right Now

Skip the code โ€” paste any Unix timestamp and get local time, UTC, and ISO 8601 instantly, free and entirely in your browser.

Open Timestamp Converter โ†’

Frequently Asked Questions

Why does JavaScript need timestamp * 1000 but Python doesn't?
JavaScript's Date object was designed to work in milliseconds from the start, while standard Unix timestamps (and Python's time/datetime module) use seconds. There's no technical reason they had to differ โ€” it's just a historical design choice each language made independently, and it's the single most common source of off-by-1000 date bugs.
How do I convert a millisecond timestamp in Python?
Divide by 1000 before passing it to datetime.fromtimestamp(): datetime.fromtimestamp(ms_timestamp / 1000). Python's function expects seconds, so a raw millisecond value (e.g. from a JavaScript API) needs to be scaled down first.
Which SQL databases have a native Unix timestamp column type?
MySQL has a dedicated TIMESTAMP column type that internally stores a Unix timestamp and converts to/from it automatically. PostgreSQL and SQL Server instead use timestamptz/datetime2 types that store an absolute point in time without directly exposing the underlying integer โ€” you convert explicitly with functions like EXTRACT(EPOCH FROM ...) or DATEDIFF when you need the raw epoch value.