Skip to content Skip to sidebar Skip to footer

Python Datetime.strptime Month Specifier Doesn't Seem To Work

The month format specifier doesn't seem to work. from datetime import datetime endDate = datetime.strptime('10 3 2011', '%j %m %Y') print endDate 2011-01-10 00:00:00 endDate = dat

Solution 1:

You can't mix the %j with others format code like %m because if you look in the table that you linked %j is the Day of the year as a decimal number [001,366] so 10 correspondent to the 10 day of the year so it's 01 of January ...

So you have just to write :

>>>datetime.strptime('10 2011', '%j %Y')
datetime.datetime(2011, 1, 10, 0, 0)

Else if you you wanted to use 10 as the day of the mount you should do :

>>>datetime.strptime('10 3 2011', '%d %m %Y')
datetime.datetime(2011, 3, 10, 0, 0)

Solution 2:

Isn't %j the "day of year" parser, which may be forcing strptime to choose January 21, overriding the %m rule?

Solution 3:

%j specifies a day of the year. It's impossible for the 10th day of the year, January 10, to occur in March, so your month specification is being ignored. Garbage In, Garbage Out.

Post a Comment for "Python Datetime.strptime Month Specifier Doesn't Seem To Work"