Page 1 of 1

datetime error in Python

Posted: Thu Oct 27, 2016 10:17 am
by pikkip
I had an error when I execute the following code:
format = '%m/%d/%y/%H:%M:%S'

t = datetime.strptime(result[a][b], format) ### result [a][b] contains a string

I use %y because the year contain only last two digits. This code works perfectly for all cases except those in which day and month are the same. For eg: 02/11/11 gives an error but 11/11/16 doesn't. Does anyone know why this happens? :frown:

Re: datetime error in Python

Posted: Thu Oct 27, 2016 11:55 am
by satoon101
It would help to see the whole code you used to produce the error and the error itself. This works perfectly fine for me in both Python2.7 and Python3.5:

Syntax: Select all

>>> from datetime import datetime

>>> datetime.strptime('11/11/11/12:22:17', '%m/%d/%y/%H:%M:%S')
datetime.datetime(2011, 11, 11, 12, 22, 17)

>>> datetime.strptime('02/11/11/12:22:17', '%m/%d/%y/%H:%M:%S')
datetime.datetime(2011, 2, 11, 12, 22, 17)

>>> datetime.strptime('11/11/16/12:22:17', '%m/%d/%y/%H:%M:%S')
datetime.datetime(2016, 11, 11, 12, 22, 17)