Strptime defaults to 1900

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 a pythonic way so that when there is no year, take the current year by default (2013)?

I checked, and the strftime function is not able to change the default value .. maybe other time libraries can do it?

thanks

+4
source share
1 answer

Parse the date as you already do, and then

 date= date.replace(2013) 

This is one of the easiest solutions using the modules you use.

Thinking about it better, you are likely to run into a problem next February 29th.

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

Source: https://habr.com/ru/post/1498284/


All Articles