Unix timestamps in Rust.
Rust's standard library gives you SystemTime and UNIX_EPOCH for Unix seconds. The chrono crate adds formatting, parsing and time zones.
Get current Unix timestamp (stdlib)
Rust's standard library is minimal for time. It gives you SystemTime but no formatting.
Seconds via std
use std::time::{SystemTime, UNIX_EPOCH};
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
// → 1735689600 (u64)
Milliseconds via std
use std::time::{SystemTime, UNIX_EPOCH};
let ms = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
// → 1735689600000 (u128)
Using the chrono crate (recommended)
For real date/time work, add the chrono crate. It handles timezones, formatting, parsing.
Cargo.toml
# Cargo.toml
[dependencies]
chrono = "0.4"
chrono-tz = "0.10" # for IANA timezone names
Current time
use chrono::Utc;
let now = Utc::now();
println!("{}", now.timestamp()); // seconds
println!("{}", now.timestamp_millis()); // milliseconds
Convert epoch to DateTime
Use DateTime::from_timestamp (chrono 0.4.31+).
Seconds → DateTime
use chrono::{DateTime, Utc};
let dt = DateTime::from_timestamp(1735689600, 0).unwrap();
println!("{}", dt.to_rfc3339());
// → "2025-01-01T00:00:00+00:00"
In a specific timezone
use chrono::DateTime;
use chrono_tz::America::New_York;
let dt = DateTime::from_timestamp(1735689600, 0)
.unwrap()
.with_timezone(&New_York);
println!("{}", dt);
Convert DateTime to epoch
DateTime has .timestamp() for seconds and .timestamp_millis() for ms.
From a specific date
use chrono::{NaiveDate, Utc, TimeZone};
let date = Utc.with_ymd_and_hms(2025, 1, 1, 0, 0, 0).unwrap();
let epoch = date.timestamp();
// → 1735689600
From a parsed string
use chrono::DateTime;
let dt: DateTime<chrono::Utc> = "2025-01-01T00:00:00Z".parse().unwrap();
println!("{}", dt.timestamp());
Format for display
chrono uses strftime-style format strings.
ISO 8601
use chrono::Utc;
let now = Utc::now();
println!("{}", now.to_rfc3339());
// → "2025-01-01T12:34:56.123456789+00:00"
Custom format
println!("{}", now.format("%Y-%m-%d %H:%M:%S"));
// → "2025-01-01 12:34:56"
Parse common formats
Use DateTime::parse_from_rfc3339 or the more flexible NaiveDateTime::parse_from_str.
RFC 3339 (ISO 8601)
use chrono::DateTime;
let dt = DateTime::parse_from_rfc3339("2025-01-01T00:00:00Z")?;
println!("{}", dt.timestamp());
Custom format
use chrono::NaiveDateTime;
let ndt = NaiveDateTime::parse_from_str(
"2025-01-01 12:00:00",
"%Y-%m-%d %H:%M:%S"
)?;
// NaiveDateTime has no timezone; attach one before using:
use chrono::Utc;
let dt = ndt.and_utc();
Common gotchas
Rust-specific time pitfalls.
SystemTime can fail
// duration_since(UNIX_EPOCH) returns Err if the clock
// went backwards. unwrap() in production is risky:
let secs = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
Naive vs aware datetimes
// chrono distinguishes NaiveDateTime (no timezone) from
// DateTime<Tz> (with timezone). Always prefer DateTime
// for code that produces or consumes Unix timestamps.
Use the converter
Need to quickly check what a specific timestamp converts to? Use the main converter: paste any value and get every format back.
Working with many timestamps at once? Try the batch converter: paste a list, get a CSV or JSON file back.