如何将u64或字符串转换为DateTime<Utc>
let timestamp_u64 = 1657113606;
let date_time = ...发布于 2022-07-06 13:29:46
有很多选择。
假设我们想要一个chrono::DateTime。偏移页建议:
在UTC结构上使用TimeZone方法是构造DateTime实例的首选方法。
我们可以使用TimeZone方法时间戳。
use chrono::{DateTime, TimeZone, Utc};
let timestamp_u64 = 1657113606;
let date_time = Utc.timestamp(timestamp_u64, 0);另一个选项使用适当命名的from_timestamp方法,但需要更多代码来完成。
use chrono::{DateTime, NaiveDateTime, Utc};
let timestamp_u64 = 1657113606;
let naive_date_time = NaiveDateTime::from_timestamp(timestamp_u64, 0);
let date_time = DateTime::<Utc>::from_utc(naive_date_time, Utc);https://stackoverflow.com/questions/72884445
复制相似问题