strptime defaulting to 1900

痞子三分冷 提交于 2021-01-27 04:44:17

问题


from datetime import datetime

datetime.strptime('%b%d  %I:%M%p', 'AUG21  3:26PM')

results with 
1900-08-21 15:26:00

how can I write in pythonic way so that when there's no year, take the current year as default (2013)?

I checked and strftime function doesn't have option to change the default.. maybe another time libraries can do?

thx


回答1:


Parse the date as you are already doing, and then

date= date.replace(2013)

This is one of simplest solution with the modules you are using.

Thinking better about it, you will probably face a problem next Feb 29.

input= 'Aug21  3:26PM'
output= datetime.datetime.strptime('2013 '+ input ,'%Y %b%d  %I:%M%p')


来源:https://stackoverflow.com/questions/18369452/strptime-defaulting-to-1900

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!