我只是想检索当前的时间和日期并将其存储在变量中。 为此,我尝试使用
chrono::DateTime
。
在文档中我发现了这个:
use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};
let dt = DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11), Utc);
这让我可以存储特定的日期和时间,但我不知道如何检索实际的当前日期和时间并将其放入我的日期时间变量中。
use chrono;
fn main() {
// returns DateTime<Local>
println!("{:?}", chrono::offset::Local::now());
// returns DateTime<Utc>
// NOTE: Available on crate feature *clock* only.
println!("{:?}", chrono::offset::Utc::now());
}
来源:
回答标题中的问题如何获取当前时间戳:
use chrono::Utc;
let dt = Utc::now();
let timestamp: i64 = dt.timestamp();
println!("Current timestamp is {}", timestamp);
如果您想使用 chrono 进行时间戳,请在 Cargo.toml 文件中添加 chrono。 前任) [依赖项] 计时=“=0.4.37”