datetime error in Python

All other Source.Python topics and issues.
User avatar
pikkip
Junior Member
Posts: 7
Joined: Mon Oct 17, 2016 11:38 am

datetime error in Python

Postby pikkip » Thu Oct 27, 2016 10:17 am

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:
User avatar
satoon101
Project Leader
Posts: 2292
Joined: Sat Jul 07, 2012 1:59 am

Re: datetime error in Python

Postby satoon101 » Thu Oct 27, 2016 11:55 am

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)
Image

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest