user266003
user266003

Reputation:

A date doesn't match the format

Why doesn't a date match the format?

>>> import datetime
>>> dt1 = "9/1/2014 0:00"
>>> datetime.datetime.strptime(dt1, "%m/%d/%y %H:%M")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '9/1/2014 0:00' does not match format '%m/%d/%y %H:%M'

And this one either:

>>> datetime.datetime.strptime(dt1, "%d/%m/%y %H:%M")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '9/1/2014 0:00' does not match format '%d/%m/%y %H:%M'

Upvotes: 0

Views: 100

Answers (2)

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

Correct syntax is,

>>> from datetime import datetime
>>> datetime.strptime('2012-09-1 0:00', '%Y-%m-%d %H:%M')
datetime.datetime(2012, 9, 1, 0, 0)
>>> 

Upvotes: 1

Bruno Gelb
Bruno Gelb

Reputation: 5652

let's open http://strftime.org/ and check:

%m  Month as a zero-padded decimal number.

so it's need to be 09, not 9

%d  Day of the month as a zero-padded decimal number.

so it's need to be 01, not 1

%y  Year without century as a zero-padded decimal number.

so it's need to be 14, not 2014

%H  Hour (24-hour clock) as a zero-padded decimal number.

so it's need to be 00, not 0.

Upvotes: 1

Related Questions