DevToolsForYou

Unix Timestamp Converter in Rust — Code Examples

Unix Timestamp Converter in RustUse the online tool →

A Unix timestamp is the number of seconds (or milliseconds) since 1 January 1970 00:00:00 UTC. Here is how to get the current timestamp, convert to a human-readable date, and parse date strings back to timestamps in each language.

Rust's std::time::SystemTime provides the raw timestamp. The chrono crate adds human-readable formatting and timezone support.

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

fn main() {
    // Current Unix timestamp
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap();
    println!("{}", now.as_secs());   // seconds
    println!("{}", now.as_millis()); // milliseconds
}
Rust (chrono crate)
// Cargo.toml: chrono = { version = "0.4", features = ["std"] }
use chrono::{DateTime, Utc, TimeZone};

fn main() {
    // Current timestamp
    let now: DateTime<Utc> = Utc::now();
    println!("{}", now.timestamp());        // Unix seconds
    println!("{}", now.timestamp_millis()); // Unix milliseconds

    // Timestamp → formatted date
    let dt = Utc.timestamp_opt(1700000000, 0).unwrap();
    println!("{}", dt.format("%Y-%m-%d %H:%M:%S UTC"));
    // 2023-11-14 22:13:20 UTC

    // Parse ISO 8601 string → timestamp
    let parsed: DateTime<Utc> = "2023-11-14T22:13:20Z".parse().unwrap();
    println!("{}", parsed.timestamp()); // 1700000000
}
Notes & gotchas
  • SystemTime::now() can theoretically return an error on some platforms — always use duration_since(UNIX_EPOCH).unwrap().
  • The chrono crate is the standard choice for date/time formatting and timezone-aware operations.
  • timestamp_opt returns a LocalResult to handle ambiguous times during DST transitions — prefer it over deprecated timestamp().
Try it in your browser

Need to epoch/unix converter without writing code? The Unix Timestamp Converter runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Epoch/Unix Converter
Unix Timestamp Converter in other languages