How to get Timestamp of the current Date and time in Rust

19,266

Solution 1

Use rust, use it now:

use chrono;

fn main() {
    println!("{:?}", chrono::offset::Local::now());
    println!("{:?}", chrono::offset::Utc::now());
}

Solution 2

To answer the question in the title of how to get the current timestamp:

use chrono::Utc;

let dt = Utc::now();
let timestamp: i64 = dt.timestamp();

println!("Current timestamp is {}", timestamp);
Share:
19,266

Related videos on Youtube

Samuel Dressel
Author by

Samuel Dressel

Updated on July 01, 2021

Comments

  • Samuel Dressel
    Samuel Dressel almost 3 years

    I simply want to retrieve the current Time and Date and store it in a variable. For this I tried to use the chrono::DateTime.

    In the documentation I found this:

    use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
    
    let dt = DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11), Utc);    
    

    This lets me store a specific Date and Time but I couldnt figure how to retrieve the actual current date and time and put it in my DateTime-Variable.