这是我格式化日期的代码:
def updateUserDBDates():
global userDB, currentDate, previousDate, changeInDate
index = 0
index2 = 0
userDB[1] = datetime.strptime("%d-%m-%Y", userDB[0])
userDB[0] = datetime.today().strftime("%d-%m-%Y")
saveData()
currentDate = userDB[0]
previousDate = userDB[1]
changeInDate = currentDate - previousDate
我收到此错误:
File "/home/nathan/Documents/project001/programFiles/Project 001.py", line 170, in updateUserDBDates
userDB[1] = datetime.strptime("%d-%m-%Y", userDB[0])
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '%d-%m-%Y' does not match format '28-09-2013'
从我所看到的一切都应该正常,导致此错误的原因以及如何轻松解决?
答案 0 :(得分:3)
datetime.datetime.strptime
收到date_str
,format
作为参数(不是format
,date_str
):
>>> import datetime
>>> datetime.datetime.strptime('28-09-2013', '%d-%m-%Y')
datetime.datetime(2013, 9, 28, 0, 0)
答案 1 :(得分:2)
strptime()
的参数排序错误。
http://docs.python.org/2/library/datetime.html#datetime.datetime.strptime