简体   繁体   中英

How to change the format of the date in Python?

I have a date as "1-Jun". How can I convert this to 01/06? I am using Strptime. I thought this is going to be easy but it is not.

Error that I am getting: time data '1-Jun' does not match format '%d-%Mmm'. This is the command I am using. Can anyone help me with this?

datetime.datetime.strptime(date, '%d-%Mmm').strftime('%m/%d')

There's no such format as %Mmm , what you need to match Jun is %b ("Locale's abbreviated month name"). Also, if you want 01/06 rather than 06/01 it is going to be '%d/%m' in strftime :

print(datetime.datetime.strptime('1-Jun', '%d-%b').strftime('%d/%m'))

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