Skip to main content
Epoch Calculator
UTC 02:47:34

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.

Current epoch (seconds)
import time
epoch = int(time.time())
Current epoch (milliseconds)
import time
epoch_ms = int(time.time() * 1000)
Epoch → datetime (UTC)
from datetime import datetime, timezone
dt = datetime.fromtimestamp(1748876543, tz=timezone.utc)
datetime → epoch
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.

Current epoch (milliseconds)
const epochMs = Date.now();
Current epoch (seconds)
const epochS = Math.floor(Date.now() / 1000);
Epoch → Date
const dt = new Date(1748876543 * 1000); // pass ms to Date()
Date → epoch (seconds)
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.

Current epoch
SELECT EXTRACT(EPOCH FROM NOW())::bigint;
Epoch → timestamp
SELECT TO_TIMESTAMP(1748876543);  -- returns timestamptz
timestamp → epoch
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2026-06-02 18:42:23')::bigint;
Extract epoch from a column
SELECT EXTRACT(EPOCH FROM created_at)::bigint FROM events;

MySQL / MariaDB

UNIX_TIMESTAMP() and FROM_UNIXTIME() are the matched pair.

Current epoch
SELECT UNIX_TIMESTAMP();
Epoch → datetime
SELECT FROM_UNIXTIME(1748876543);
datetime → epoch
SELECT UNIX_TIMESTAMP('2026-06-02 18:42:23');

BigQuery (Google Cloud)

UNIX_SECONDS, UNIX_MILLIS, TIMESTAMP_SECONDS: explicit unit in every name.

Current epoch (seconds)
SELECT UNIX_SECONDS(CURRENT_TIMESTAMP());
Epoch → timestamp
SELECT TIMESTAMP_SECONDS(1748876543);
timestamp → epoch
SELECT UNIX_SECONDS(TIMESTAMP '2026-06-02 18:42:23 UTC');

Snowflake

DATE_PART(EPOCH_SECOND, …) and TO_TIMESTAMP(<int>).

Current epoch
SELECT DATE_PART(EPOCH_SECOND, CURRENT_TIMESTAMP());
Epoch → timestamp
SELECT TO_TIMESTAMP(1748876543);
timestamp → epoch
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.

Current epoch
date +%s
Epoch → date (GNU date; Linux)
date -d @1748876543 -u
Epoch → date (BSD date; macOS)
date -ur 1748876543
Date → epoch (GNU)
date -d "2026-06-02 18:42:23 UTC" +%s

Java (8+) / Kotlin

java.time.Instant is the modern way. System.currentTimeMillis is the old way.

Current epoch (seconds)
long epoch = Instant.now().getEpochSecond();
Current epoch (milliseconds)
long epochMs = System.currentTimeMillis();
Epoch → Instant
Instant dt = Instant.ofEpochSecond(1748876543L);
Date → epoch
long epoch = Instant.parse("2026-06-02T18:42:23Z").getEpochSecond();

Go

time.Now().Unix() and time.Unix() are the canonical pair.

Current epoch (seconds)
epoch := time.Now().Unix()
Current epoch (milliseconds)
epochMs := time.Now().UnixMilli()
Epoch → time.Time
dt := time.Unix(1748876543, 0).UTC()
Date → epoch
dt, _ := time.Parse(time.RFC3339, "2026-06-02T18:42:23Z")
epoch := dt.Unix()

Rust

std::time + chrono for the timezone-aware bits.

Current epoch (seconds)
use std::time::{SystemTime, UNIX_EPOCH};
let epoch = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
Epoch → DateTime (chrono crate)
use chrono::{DateTime, Utc};
let dt: DateTime<Utc> = DateTime::from_timestamp(1748876543, 0).unwrap();

Ruby

Time#to_i and Time.at.

Current epoch
epoch = Time.now.to_i
Epoch → Time
dt = Time.at(1748876543).utc
Date → epoch
epoch = Time.parse('2026-06-02T18:42:23Z').to_i

PHP

time() returns seconds. DateTimeImmutable is the modern API.

Current epoch
$epoch = time();
Epoch → date string
$date = gmdate('c', 1748876543);  // ISO 8601 UTC
Date → epoch
$epoch = strtotime('2026-06-02T18:42:23Z');

C#

DateTimeOffset.ToUnixTimeSeconds (since .NET 4.6).

Current epoch (seconds)
long epoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
Epoch → DateTimeOffset
var dt = DateTimeOffset.FromUnixTimeSeconds(1748876543);

Excel / Google Sheets

Spreadsheets count days since 1899-12-30, not seconds since 1970.

Unix epoch (seconds) → date
=A1/86400 + DATE(1970,1,1)
Date → Unix epoch (seconds)
=(A1 - DATE(1970,1,1)) * 86400
Unix epoch (milliseconds) → date
=A1/86400000 + DATE(1970,1,1)

See also

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.