Epoch time in code
How to get the current Unix timestamp, convert epoch to a date, and convert a date back to an epoch; in 14 languages and runtimes. Every snippet is idiomatic and standard-library where possible. Numbers use the example value 1748876543 = 2026-06-02T18:42:23Z.
Python
time, datetime, and the timezone-aware way to do this in Python 3.
import time
epoch = int(time.time())import time
epoch_ms = int(time.time() * 1000)from datetime import datetime, timezone
dt = datetime.fromtimestamp(1748876543, tz=timezone.utc)from datetime import datetime, timezone
epoch = int(datetime(2026, 6, 2, 18, 42, 23, tzinfo=timezone.utc).timestamp())JavaScript / TypeScript
Date.now() returns milliseconds. Most APIs want seconds.
const epochMs = Date.now();const epochS = Math.floor(Date.now() / 1000);const dt = new Date(1748876543 * 1000); // pass ms to Date()const epoch = Math.floor(
new Date('2026-06-02T18:42:23Z').getTime() / 1000
);PostgreSQL
EXTRACT(EPOCH FROM …) is the canonical way. TO_TIMESTAMP for the reverse.
SELECT EXTRACT(EPOCH FROM NOW())::bigint;SELECT TO_TIMESTAMP(1748876543); -- returns timestamptzSELECT EXTRACT(EPOCH FROM TIMESTAMP '2026-06-02 18:42:23')::bigint;SELECT EXTRACT(EPOCH FROM created_at)::bigint FROM events;MySQL / MariaDB
UNIX_TIMESTAMP() and FROM_UNIXTIME() are the matched pair.
SELECT UNIX_TIMESTAMP();SELECT FROM_UNIXTIME(1748876543);SELECT UNIX_TIMESTAMP('2026-06-02 18:42:23');BigQuery (Google Cloud)
UNIX_SECONDS, UNIX_MILLIS, TIMESTAMP_SECONDS: explicit unit in every name.
SELECT UNIX_SECONDS(CURRENT_TIMESTAMP());SELECT TIMESTAMP_SECONDS(1748876543);SELECT UNIX_SECONDS(TIMESTAMP '2026-06-02 18:42:23 UTC');Snowflake
DATE_PART(EPOCH_SECOND, …) and TO_TIMESTAMP(<int>).
SELECT DATE_PART(EPOCH_SECOND, CURRENT_TIMESTAMP());SELECT TO_TIMESTAMP(1748876543);SELECT DATE_PART(EPOCH_SECOND, '2026-06-02 18:42:23'::TIMESTAMP);Linux / macOS shell (bash, zsh)
GNU date and BSD date diverge on the syntax for converting from epoch.
date +%sdate -d @1748876543 -udate -ur 1748876543date -d "2026-06-02 18:42:23 UTC" +%sJava (8+) / Kotlin
java.time.Instant is the modern way. System.currentTimeMillis is the old way.
long epoch = Instant.now().getEpochSecond();long epochMs = System.currentTimeMillis();Instant dt = Instant.ofEpochSecond(1748876543L);long epoch = Instant.parse("2026-06-02T18:42:23Z").getEpochSecond();Go
time.Now().Unix() and time.Unix() are the canonical pair.
epoch := time.Now().Unix()epochMs := time.Now().UnixMilli()dt := time.Unix(1748876543, 0).UTC()dt, _ := time.Parse(time.RFC3339, "2026-06-02T18:42:23Z")
epoch := dt.Unix()Rust
std::time + chrono for the timezone-aware bits.
use std::time::{SystemTime, UNIX_EPOCH};
let epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();use chrono::{DateTime, Utc};
let dt: DateTime<Utc> = DateTime::from_timestamp(1748876543, 0).unwrap();Ruby
Time#to_i and Time.at.
epoch = Time.now.to_idt = Time.at(1748876543).utcepoch = Time.parse('2026-06-02T18:42:23Z').to_iPHP
time() returns seconds. DateTimeImmutable is the modern API.
$epoch = time();$date = gmdate('c', 1748876543); // ISO 8601 UTC$epoch = strtotime('2026-06-02T18:42:23Z');C#
DateTimeOffset.ToUnixTimeSeconds (since .NET 4.6).
long epoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();var dt = DateTimeOffset.FromUnixTimeSeconds(1748876543);Excel / Google Sheets
Spreadsheets count days since 1899-12-30, not seconds since 1970.
=A1/86400 + DATE(1970,1,1)=(A1 - DATE(1970,1,1)) * 86400=A1/86400000 + DATE(1970,1,1)See also
- Unix timestamp converter , decode any epoch in your browser.
- What is epoch time? , concepts and history.
- ISO 8601 validator + builder , every variant of the standard date-time string.
- Cron expression tester , for the other end of the scheduling pipeline.
Frequently asked questions
How do I get the current epoch in Python?
Use int(time.time()) for seconds, or int(time.time() * 1000) for milliseconds. The time module is part of the standard library and requires no install. If you want a timezone-aware datetime instead of a raw integer, use datetime.now(timezone.utc).
How do I extract epoch from a timestamp in PostgreSQL?
Use EXTRACT(EPOCH FROM <timestamp>)::bigint. The result is the number of seconds since 1970-01-01 UTC as a 64-bit integer. The double-colon cast to bigint drops the fractional seconds; leave it off if you need millisecond precision.
How do I convert epoch to timestamp in BigQuery?
Use TIMESTAMP_SECONDS(epoch_int) for seconds or TIMESTAMP_MILLIS(epoch_int) for milliseconds. The reverse direction uses UNIX_SECONDS(timestamp) or UNIX_MILLIS(timestamp). BigQuery is unusual in naming the unit explicitly in every function; it removes a common source of bugs.
How do I convert epoch to date in Linux bash?
On GNU date (most Linux distributions): date -d @1748876543 -u. On BSD date (macOS): date -ur 1748876543. The two utilities are not compatible; scripts that need to run on both should detect which is available, or use python -c instead.
Why does JavaScript Date.now() return milliseconds when Unix time is in seconds?
It is a historical inconsistency. Most languages and most APIs use seconds, but JavaScript chose milliseconds when Date was added, and changing it later would break the web. The common pattern is Math.floor(Date.now() / 1000) when calling a seconds-based API.