如何使用Python计算两个日期之间的天数?
如果我有两个日期(例如'8/18/2008'
和'9/26/2008'
),获得这两个日期之间的天数的最佳方法是什么?
如果你有两个日期对象,你可以减去它们。
from datetime import date
d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print delta.days
文档的相关部分:https://docs.python.org/library/datetime.html
使用日期时间的力量:
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2008', date_format)
delta = b - a
print delta.days # that's it
圣诞节前夕:
>>> import datetime
>>> today = datetime.date.today()
>>> someday = datetime.date(2008, 12, 25)
>>> diff = someday - today
>>> diff.days
86
更多算术在这里。
链接地址: http://www.djcxy.com/p/36705.html上一篇: How do I calculate number of days between two dates using Python?
下一篇: Why doesn't subtracting two local DateTime values appear to account for Daylight Saving Time?