【问题标题】:How to find difference in time from current date and a pre-determined date in python? [duplicate]如何在python中找到当前日期和预定日期的时间差? [复制]
【发布时间】:2011-11-06 14:38:51
【问题描述】:

可能重复:
How do I find the time difference between two datetime objects in python?

我正在尝试在 python 中查找当前时间和给定时间之间的天数差异。

例如,给出的时间是 2011-07-20,发布此问题时的天数相差 41 天。

所以我的问题是,我如何将给定的时间转换成可以用来计算时差的时间。我正在尝试使用 datetime.timedelta.now() - 给定时间。

谢谢!

【问题讨论】:

    标签: python datetime


    【解决方案1】:

    当然是这样的:

    from datetime import datetime
    
    d = datetime.now()
    d1 = datetime.strptime('2011-07-20', '%Y-%m-%d')
    
    (d-d1).days
    >>> 42 # number of total days beetween two given dates
    

    在 strptime using the following link 上查找更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      相关资源
      最近更新 更多