简体   繁体   中英

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'

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")

This is one solution:

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

Output:

'29/01/2021'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM