[英]Python datetime.strptime - ValueError: time data does not match format
[英]Date does not match format in python datetime.strptime
如果MINYEAR
可能为1,为什么在Python datetime.strptime
出现错误? 我的设置是Mac OSX 10.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'
%Y
格式只能使用四位数字使用几年:
>>> datetime.strptime('0001.1.1', '%Y.%m.%d')
datetime.datetime(1, 1, 1, 0, 0)
您可以使用str.zfill()
填充'date'字符串:
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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.