【问题标题】:How to find the difference between 2 NaiveDateTimes?如何找到 2 NaiveDateTimes 之间的差异?
【发布时间】:2018-06-27 00:34:53
【问题描述】:

我正在使用chrono。我有now() 和其他一些NaiveDateTime。我怎样才能找到它们之间的区别?

let now = Utc::now().naive_utc();
let dt1 = get_my_naive_datetime();

【问题讨论】:

    标签: datetime rust timestamp rust-chrono


    【解决方案1】:

    使用NaiveDateTime::signed_duration_since:

    println!("{:?}", dt1.signed_duration_since(now))
    

    (playground)

    它返回一个Duration,它有&self-获取任何你喜欢的单位的方法,例如。 dt1.signed_duration_since(now).num_days().

    【讨论】:

    • 如何在天、小时、分钟内得到它?
    • @Kurama 有什么问题?此方法返回 Duration 对象,其中包含您想要的所有方法, - 获取天数、小时数和分钟数。只需打开该链接,您就会看到。
    • 或者您可以简单地使用减法从两个NaiveDateTime 实例中获得Durationnow-dt1
    • @Jmb 这是我的第一个想法,但Sub<NaiveDateTime> 没有为NaiveDateTime 实现。
    • @trentcl 哎呀,我的错,我读文档有点太快了:我看到 Sub<Something> 是为 NaiveDateTime 实现的,我没有注意 Something 实际上是什么是(在这种情况下为FixedOffset
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多