如果Python datetime.strptime可能为1,为什么MINYEAR中有一个错误?我的设置是MacOSX10.10.2 Python 3.4。
from datetime import *
def days_diff(date1, date2):
c=datetime.strptime('.'.join(str(i) for i in date1),"%Y.%m.%d")
d=datetime.strptime('.'.join(str(i) for i in date2),"%Y.%m.%d")
return print(c,d)
days_diff((1, 1, 1), (9999,12,31))
ValueError: time data '1.1.1' does not match format '%Y.%m.%d'发布于 2014-12-26 20:45:01
%Y格式使用四位数只工作数年。
>>> datetime.strptime('0001.1.1', '%Y.%m.%d')
datetime.datetime(1, 1, 1, 0, 0)您可以使用str.zfill()删除您的“日期”字符串:
c = datetime.strptime('.'.join(str(i) for i in date1).zfill(8), "%Y.%m.%d")
d = datetime.strptime('.'.join(str(i) for i in date2).zfill(8), "%Y.%m.%d")https://stackoverflow.com/questions/27661776
复制相似问题