Search code examples
python2-digit-yeary2k

Convert date format: 2 digit year to 4 digit year


If I have a string that contain a date, like '29/01/21', how can I convert its format to a 4 year digit instead of 2 digits?

'29/01/21'

to

'29/01/2021'

Solution

  • Pythons datetime library might be helpful.

    Please refer below code.

    from datetime import datetime
    
    datetime.datetime.strptime('29/01/21', '%d/%m/%y').strftime("%M/%d/%Y")