Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date formatting to month uppercase

I managed to get date by

import datetime
getDate = datetime.date.today()
print(getDate.strftime("%Y-%B-%d"))

Output is 2018-June-23

But I want to format output like this: 2018-JUNE-23 (month is uppercase)

like image 216
CodeKeeper Avatar asked Dec 08 '25 00:12

CodeKeeper


1 Answers

To do this directly in the format string, prepend a carrot on the month (^):

>>> getDate = datetime.date.today()
>>> print(getDate.strftime("%Y-%^B-%d"))
2018-JUNE-22

Note: This works if you have the glibc extensions (or equivalent features) available on your platform strftime. You can check for that by calling man strftime. If it's not working on your platform, or you need to guarantee the behaviour cross-platform, then prefer to just make an extra function call here by using str.upper as shown in the other answer.

like image 111
wim Avatar answered Dec 10 '25 14:12

wim



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!