【问题标题】:calculate the days between two times in python with presicion value用精度值计算python中两次之间的天数
【发布时间】:2018-06-08 12:47:29
【问题描述】:

我有两次像 t1= "2018-04-28 10:32:32+00:00" 和 t2= "2018-04-29 20:32:32+00:00" 需要找出这两天之间的天数。实际上在上面两天有 36(意味着 1 天和半天)小时的差异,所以我需要输出为 1.5(意味着 1 天和半天)。使用 python 如何实现这一点

【问题讨论】:

标签: python date


【解决方案1】:

您可以在datetime 模块中使用timedelta。我使用parse 进行更多日期解析。

In [2]: from dateutil.parser import parse

In [3]: d1 = parse("2018-04-28 10:32:32+00:00")

In [4]: d2 = parse("2018-04-29 20:32:32+00:00")

In [5]: de = d2 - d1

In [6]: de.total_seconds()/ (60 * 60 * 24)
Out[6]: 1.4166666666666667

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多