In Sweden we sometimes use a strange date format, for example New Year is the 31/12. If I have this format as a string ,which can be any date between 1/1 and 31/12, and if we assume that it is this year, how do I get into a standard date format using Python (format as 2012-01-01 and 2012-12-31) that can be sore in be stored as a date in a mySQL database.
Share
Simply split the two values, map them to integers and update a
datetime.date()instance:By using
datetime.date.today()we get the current year.Demo:
Alternatively, you could use the
datetime.strptime()method to parse these dates, but you’ll have to manually correct the year afterwards (it’ll use 1900 as the default if no year is being parsed):