Unix Timestamp in Rust

Rust's standard library provides SystemTime for Unix timestamps. For richer date handling, the chrono crate is the community standard.

Code Examples

Current timestamp (seconds) — std only

use std::time::SystemTime;

fn main() {
    let secs = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    println!("{secs}");
}

No external crates needed. duration_since returns a Duration — .as_secs() gives u64 seconds since epoch.

Current timestamp (milliseconds) — std only

use std::time::SystemTime;

let ms = SystemTime::now()
    .duration_since(SystemTime::UNIX_EPOCH)
    .unwrap()
    .as_millis(); // u128

as_millis() returns u128 — note the wider type, since u64 would overflow for milliseconds around year 2554.

Using chrono crate (recommended)

// Cargo.toml: chrono = "0.4"
use chrono::Utc;

let secs = Utc::now().timestamp();
let ms   = Utc::now().timestamp_millis();

chrono is the standard date/time crate in the Rust ecosystem. timestamp() returns i64 seconds; timestamp_millis() returns i64 milliseconds.

Convert timestamp back to DateTime (chrono)

use chrono::{DateTime, Utc, TimeZone};

let dt: DateTime<Utc> = Utc.timestamp_opt(1708560000, 0).unwrap();
println!("{}", dt.to_rfc3339());

Reconstruct a DateTime from a Unix timestamp using chrono's TimeZone trait.

Note

Rust's std SystemTime uses u64 for seconds and u128 for milliseconds — both unsigned. chrono uses i64 (signed) to support dates before 1970. For production code, prefer chrono over manual Duration arithmetic.

Main Epoch Converter

Use the homepage when you need epoch to date, date to epoch, or a full epoch converter

This Rust guide shows how to create and parse Unix timestamps in code. If your next step is broader search intent like epoch converter, epoch to date, epoch time to date, or unix epoch converter, jump back to the live homepage tools to paste a raw Unix value, auto-detect seconds versus milliseconds, and copy the readable answer instantly.

Related Tools

More free tools for developers debugging timestamps, schedules, and text changes.