【问题标题】:How to compare two dates in python如何在python中比较两个日期
【发布时间】:2022-01-05 11:45:20
【问题描述】:

我有 2 个日期我想要 2 个日期之间的天数如何检索

expiry_date = details['expires']
today = date.today().strftime("%d/%m/%Y")

例如

28/11/2021
29/11/2021
no.of.days=1

【问题讨论】:

标签: python python-3.x datetime


【解决方案1】:

如果您有两个datetime 对象,您可以将它们相减得到timedelta

diff = (datetime.datetime.strptime("29/11/2021", "%d/%m/%Y") - 
        datetime.datetime.strptime("28/11/2021", "%d/%m/%Y"))

然后您可以转换为天数:

diff.total_seconds() / 86400.

【讨论】:

    【解决方案2】:

    先自己找答案,找不到就问。

    How to calculate number of days between two given dates

    使用这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-13
      • 2021-08-17
      • 2011-04-20
      • 1970-01-01
      • 2016-06-25
      相关资源
      最近更新 更多