我想知道以下日期之间的天数差异......是否有人可以提供有关如何实现这一目标的信息?
CR created date
2013-11-01
Current date
2013-11-09 18:17:53.196126
答案 0 :(得分:2)
首先,您必须将输入更改为python知道的类型 - datetime
。然后使用内置函数。
>>> from datetime import datetime
>>> A = datetime.strptime('2013-11-01', '%Y-%m-%d')
>>> A
datetime.datetime(2013, 11, 1, 0, 0)
>>> B = datetime.strptime('2013-11-09 18:17:53.196126', '%Y-%m-%d %H:%M:%S.%f')
>>> B
datetime.datetime(2013, 11, 9, 18, 17, 53, 196126)
>>> diff = B - A
>>> diff
datetime.timedelta(8, 65873, 196126)
>>> diff.total_seconds()
757073.196126
>>> diff.total_seconds() / (60 * 60 * 24)
8.762421251458333
答案 1 :(得分:1)
使用datetime
模块。如果您有datetime.datetime
对象A
和datetime.date
对象B
,则区别在于:
A.date() - B
试一试; - )
示例:
>>> from datetime import datetime, date
>>> A = datetime.strptime("2013-11-09 18:17:53.196126", "%Y-%m-%d %H:%M:%S.%f")
>>> B = date(*map(int, "2013-11-01".split("-")))
>>> print A
2013-11-09 18:17:53.196126
>>> print B
2013-11-01
>>> print A.date() - B
8 days, 0:00:00