Skip to main content
 首页 » 编程设计

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

2025年05月04日25mayingbao

这将在 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 

但是在2.4中显示如下错误

AttributeError: type object 'datetime.datetime' has no attribute 'strptime' 

感谢大家的支持。

从日期时间导入日期 从时间导入 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 

请您参考如下方法:

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

from time import strptime 

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