G.S. J
G.S. J

Reputation: 233

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

Upvotes: 0

Views: 33

Answers (1)

Błotosmętek
Błotosmętek

Reputation: 12927

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

Upvotes: 2

Related Questions