All guides

How to get and convert a Unix timestamp in JavaScript, Python, Java, Go, SQL and more

Updated

A quick reference for the two things you need most often: getting now as a Unix timestamp, and turning a timestamp back into a date. Each snippet notes its unit, because seconds and milliseconds are easy to mix up — see the units guide if you are unsure which you have.

JavaScript / TypeScript

JavaScript
// now
Date.now();                           // milliseconds
Math.floor(Date.now() / 1000);        // seconds

// timestamp -> date
new Date(1700000000 * 1000);          // from seconds
new Date(1700000000000).toISOString(); // "2023-11-14T22:13:20.000Z"

// date -> timestamp
Math.floor(new Date("2025-01-01T00:00:00Z").getTime() / 1000); // 1735689600

JavaScript Date objects always store milliseconds since the epoch internally. performance.now() is not an epoch timestamp — it measures time since the page loaded.

Python

Python
import time
from datetime import datetime, timezone

time.time()            # seconds, float: 1700000000.123456
int(time.time())       # seconds, int
time.time_ns()         # nanoseconds, int (Python 3.7+)

datetime.fromtimestamp(1700000000, tz=timezone.utc)
# datetime(2023, 11, 14, 22, 13, 20, tzinfo=timezone.utc)

datetime(2025, 1, 1, tzinfo=timezone.utc).timestamp()   # 1735689600.0

Always pass tz=timezone.utc (or another zone). Without it, fromtimestamp returns a naive local time, and calling .timestamp() on a naive datetime assumes it is in the machine’s local zone — a frequent source of hour-sized bugs on servers.

Java

Java
import java.time.Instant;

System.currentTimeMillis();            // milliseconds
Instant.now().getEpochSecond();        // seconds

Instant.ofEpochSecond(1700000000);     // 2023-11-14T22:13:20Z
Instant.parse("2025-01-01T00:00:00Z").getEpochSecond(); // 1735689600

Go

Go
now := time.Now()
now.Unix()       // seconds
now.UnixMilli()  // milliseconds (Go 1.17+)
now.UnixNano()   // nanoseconds

time.Unix(1700000000, 0).UTC() // 2023-11-14 22:13:20 +0000 UTC

C# / .NET

C#
DateTimeOffset.UtcNow.ToUnixTimeSeconds();       // seconds
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();  // milliseconds

DateTimeOffset.FromUnixTimeSeconds(1700000000);  // 14/11/2023 22:13:20 +00:00

PHP

PHP
time();              // seconds
microtime(true);     // seconds as float, microsecond precision

gmdate('c', 1700000000);         // "2023-11-14T22:13:20+00:00"
strtotime('2025-01-01 00:00 UTC'); // 1735689600

Ruby

Ruby
Time.now.to_i            # seconds
Time.at(1700000000).utc  # 2023-11-14 22:13:20 UTC

Rust

Rust
use std::time::{SystemTime, UNIX_EPOCH};

let secs = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs();          // seconds (u64); use .as_millis() for ms

Bash / command line

Shell
date +%s                    # seconds (GNU and macOS)
date +%s%3N                 # milliseconds (GNU date only)

date -u -d @1700000000      # GNU/Linux: timestamp -> date
date -u -r 1700000000       # macOS/BSD: timestamp -> date

PostgreSQL

SQL
SELECT EXTRACT(EPOCH FROM now());   -- seconds, with fractional part
SELECT to_timestamp(1700000000);      -- timestamptz 2023-11-14 22:13:20+00

MySQL

SQL
SELECT UNIX_TIMESTAMP();             -- seconds
SELECT FROM_UNIXTIME(1700000000);    -- shown in the session time zone

FROM_UNIXTIME renders the result in the connection’s time_zone setting, so the same query can show different wall-clock times on different servers. Set SET time_zone = '+00:00' if you want UTC.

SQLite

SQL
SELECT strftime('%s', 'now');            -- seconds, as text
SELECT unixepoch();                      -- seconds, as integer (SQLite 3.38+)
SELECT datetime(1700000000, 'unixepoch'); -- '2023-11-14 22:13:20' (UTC)