Unix timestamps in Rust

Everything Rust needs for Unix timestamps: the current epoch time in seconds and milliseconds, timestamp-to-date and date-to-timestamp conversion, and ISO 8601 parsing. The standard library gives you the raw duration since the epoch; for calendars, formatting, and parsing, the ecosystem standard is chrono (or the newer jiff). There's a live converter at the bottom for checking any value the snippets produce.

Get the current Unix timestamp (std only)

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

let seconds = SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .expect("clock before 1970")
    .as_secs(); // 1700000000

With chrono

use chrono::Utc;

let seconds = Utc::now().timestamp();        // 1700000000
let millis = Utc::now().timestamp_millis();  // 1700000000000

Convert a timestamp to a DateTime

use chrono::DateTime;

let dt = DateTime::from_timestamp(1700000000, 0)
    .expect("out of range");
// 2023-11-14 22:13:20 UTC

Parse and format ISO 8601

use chrono::{DateTime, Utc};

let dt: DateTime<Utc> = "2023-11-14T22:13:20Z".parse()?;
let s = dt.to_rfc3339(); // "2023-11-14T22:13:20+00:00"

Check a value

Detected: Unix timestamp (seconds)

Your timezone

UTC · GMT+00:00 · UTC

Tue, Nov 14, 2023, 10:13:20 PM

Common formats

UTC

Tue, Nov 14, 2023, 10:13:20 PM

ISO 8601

2023-11-14T22:13:20.000Z

Unix (seconds)

1700000000

Unix (millis)

1700000000000

Relative

World clock

Los Angeles PST

Tue, Nov 14, 2023, 02:13:20 PM

GMT-08:00

Chicago CST

Tue, Nov 14, 2023, 04:13:20 PM

GMT-06:00

New York EST

Tue, Nov 14, 2023, 05:13:20 PM

GMT-05:00

London GMT

Tue, Nov 14, 2023, 10:13:20 PM

GMT+00:00

Paris CET

Tue, Nov 14, 2023, 11:13:20 PM

GMT+01:00

Kolkata IST

Wed, Nov 15, 2023, 03:43:20 AM

GMT+05:30

Shanghai CST

Wed, Nov 15, 2023, 06:13:20 AM

GMT+08:00

Tokyo JST

Wed, Nov 15, 2023, 07:13:20 AM

GMT+09:00

Sydney AEDT

Wed, Nov 15, 2023, 09:13:20 AM

GMT+11:00

Common questions

Do I need chrono at all?
For a bare timestamp number, no: std's SystemTime does it. The moment you need formatting, parsing, calendars, or timezones, yes: std has no calendar logic by design.
Can duration_since(UNIX_EPOCH) really fail?
Only if the system clock reads before 1970, which misconfigured hardware does produce. Library code should handle the Err rather than unwrap.
What about timezones beyond UTC?
chrono handles fixed offsets; for real IANA zones (DST rules) add chrono-tz, or use jiff, which bundles the timezone database.

Related converters