Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: convert date format YYYY-mm-dd to dd-MON-yyyy with abbreviated month

Tags:

python

I have a date that is a string in this format:

'2021-01-16'

And need to convert it to a string in this format:

'16-JAN-2021'

I am able to get most of it like this:

x = datetime.strptime('2021-01-16', '%Y-%m-%d')
x.strftime('%d-%b-%Y')

But the month is not fully capitalized:

'16-Jan-2021'
like image 503
sanjayr Avatar asked Oct 20 '25 14:10

sanjayr


1 Answers

Just use upper() to capitalize the output string:

from datetime import datetime

x = datetime.strptime('2021-01-16', '%Y-%m-%d')

print(x.strftime('%d-%b-%Y').upper())
# 16-JAN-2021
like image 61
Kien Nguyen Avatar answered Oct 22 '25 06:10

Kien Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!