HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrustMajor

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howtimestampanddatetimethecurrentrustget

Problem

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::::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 couldn't figure out how to retrieve the actual current date and time and put it in my DateTime-Variable.

Solution

use chrono;

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

// returns DateTime
// NOTE: Available on crate feature clock only.
println!("{:?}", chrono::offset::Utc::now());
}


Rust playground

Sources:

chrono::offset::Local#method.now

chrono::offset::Utc#method.now

Context

Stack Overflow Q#57707966, score: 93

Revisions (0)

No revisions yet.