python 2.4中两个日期之间的天数

Jib*_*bin 1 python strptime

这将在python 2.6中工作

from datetime import datetime
print (datetime.strptime('2008-12-31', "%Y-%m-%d")- datetime.strptime('2007-04-30',"%Y-%m-%d")).days
Run Code Online (Sandbox Code Playgroud)

但在2.4中显示以下错误

AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
Run Code Online (Sandbox Code Playgroud)

感谢你的支持.

从datetime导入日期的时间导入strptime

x=  strptime('2008-12-31', "%Y-%m-%d")
y=  strptime('2007-04-30', "%Y-%m-%d")

print (date(x.tm_year,x.tm_mon,x.tm_mday)- date(y.tm_year,y.tm_mon,y.tm_mday)).days
Run Code Online (Sandbox Code Playgroud)

Zau*_*bov 7

在Python 2.4中,strptime位于时间模块中:

from time import strptime
Run Code Online (Sandbox Code Playgroud)

请参见http://docs.python.org/release/2.4/lib/module-time.html