Zeno
Zeno

Reputation: 1839

Python: strptime not matching format

ValueError: time data '03-10-2011 04:35 PM' does not match format '%m-%d-%Y %I:M %p'

That looks like it matches to me?

 datetime = datestr + " " + timestr
 date_struct = time.strptime(datetime, "%m-%d-%Y %I:M %p")

Upvotes: 1

Views: 4301

Answers (2)

John Machin
John Machin

Reputation: 83022

Those datetime format strings work both ways, so you can easily check hypotheses such as "looks like it matches":

>>> import datetime
>>> fmt = "%m-%d-%Y %I:M %p"
>>> dt = datetime.datetime(2011, 3, 10, 16, 35)
>>> dt.strftime(fmt)
'03-10-2011 04:M PM' # Oops!
>>>

Upvotes: 4

Ocaso Protal
Ocaso Protal

Reputation: 20267

You are missing a % right before the M:

 date_struct = time.strptime(datetime, "%m-%d-%Y %I:%M %p")

Upvotes: 8

Related Questions