CCKx
CCKx

Reputation: 1343

Why am I getting this time data does not match format error?

The original text I'm parsing is Sep 12, 2014 15:26:47 MDT

Since parsing the timezone doesn't work, I remove it by using this

d = " ".join(input.split()[:-1])

and then try to convert it to a datetime object with this

d = datetime.datetime.strptime(d, "%b %d, %Y %I:%M:%S")

but I keep getting this error:

time data 'Sep 12, 2014 15:26:47' does not match format '%b %d, %Y %I:%M:%S'

What's wrong here?

Upvotes: 0

Views: 598

Answers (2)

gkrls
gkrls

Reputation: 2664

Change this:

d = datetime.datetime.strptime(d, "%b %d, %Y %I:%M:%S")

to

d = datetime.datetime.strptime(d, "%b %d, %Y %H:%M:%S")

Upvotes: 1

Malcolm Murdoch
Malcolm Murdoch

Reputation: 1085

Aha %I gets you the 12-hour hour (in your example, 03).

You need to use %H

 d = datetime.datetime.strptime(d, "%b %d, %Y %H:%M:%S")

Worth bookmarking this page: https://docs.python.org/2/library/datetime.html

Upvotes: 2

Related Questions