Python datetime.strptime month specifier doesn't seem to work

后端 未结 3 1117
北海茫月
北海茫月 2020-12-21 08:17

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-0         


        
3条回答
  •  萌比男神i
    2020-12-21 09:06

    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)
    

提交回复
热议问题