Johnny Lastre
Johnny Lastre

Reputation: 81

Python time data does not match format

I have string date data with the following format:

4/16/15 23:50

When I try to convert into a datetime object:

print datetime.datetime.strptime(fecha2, '%d/%m/%y %H:%M')

I get this error:

ValueError: time data '4/16/15 23:50' does not match format '%d/%m/%y %H:%M'

According to this list: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior, I am using the right format. Where am I wrong?

Upvotes: 0

Views: 9613

Answers (3)

Alan
Alan

Reputation: 652

I know this topic is old but maybe it will help someone. I have faced recently similar issue where the date was actually correctly typed and it throwed the same error:

datetime.datetime.strptime('2018-05-02T14:27:56+00:00', "%Y-%m-%dT%H:%M:%S%z")

error: time data '2018-05-02T14:27:56+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'

The issue was that i was trying to run the python 3.7 code with python 3.6

Upvotes: 1

stewart
stewart

Reputation: 189

Also very handy:

from dateutil.parser import parse
parse("4/16/15 23:50")

Upvotes: 3

Alexander
Alexander

Reputation: 109510

you have month and day reversed:

print datetime.datetime.strptime(fecha2, '%m/%d/%y %H:%M')

Upvotes: 6

Related Questions