Unix Timestamp in Rust

Use the `chrono` crate - the de-facto standard for date/time work. The std library `SystemTime` works for simple cases.

Common Operations

Get current Unix timestamp (chrono)

use chrono::Utc;
let ts = Utc::now().timestamp();
// 1706745600

Get current Unix timestamp (std)

use std::time::{SystemTime, UNIX_EPOCH};
let ts = SystemTime::now()
    .duration_since(UNIX_EPOCH).unwrap()
    .as_secs();

Convert Unix timestamp to DateTime

use chrono::{DateTime, Utc};
let dt = DateTime::<Utc>::from_timestamp(1706745600, 0).unwrap();
// 2024-01-31T22:40:00Z

Format as ISO 8601

use chrono::Utc;
let iso = Utc::now().to_rfc3339();
// '2024-01-31T22:40:00.123456Z'

Parse ISO 8601

use chrono::DateTime;
let dt: DateTime<Utc> = "2024-01-31T12:00:00Z".parse().unwrap();

Other Languages

Need the full converter? Open the Timestamp Converter →