Parse string date without padding zero into python date object

48 views Asked by At

date = '10624'.

here,

1 is month i.e. Jan,

06 is day,

24 is Year.

I don't want to use any formatting on string (like add 0 at start in string or strip this string into small parts).

I want to convert this string into python date object directly using strftime.

%-m%d%y is not working and %m%d%y is giving wrong output(month is parsing as 10 and not 01)

1

There are 1 answers

0
Purushottam Nawale On

You still need some formatting.

from datetime import datetime

date_str = '10624'
date_obj = datetime.strptime(date_str.zfill(6), '%m%d%y')
date_format1 = date_obj.strftime("%d-%m-%Y")
print(date_format1) # 06-01-2024