Convert date format: 2 digit year to 4 digit year

2k Views Asked by At

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'
2

There are 2 best solutions below

0
shajahan On BEST ANSWER

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")
0
ph140 On

This is one solution:

mystring = '29/01/21'
mystring = mystring[:6]+'20'+mystring[-2:]
print(mystring)

Output:

'29/01/2021'