-3

这是我格式化日期的代码:

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'

从我所看到的一切应该可以正常工作,是什么导致了这个错误,我怎样才能轻松地修复它?

4

2 回答 2

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)
于 2013-09-28T17:24:11.247 回答
2

参数排序strptime()是错误的。

http://docs.python.org/2/library/datetime.html#datetime.datetime.strptime

于 2013-09-28T17:24:52.717 回答