Reputation: 83
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)
Upvotes: 3
Views: 19505
Reputation: 95
To add on to the answer by wim, in order to make it cross-platform, an additional wrapper is the most accurate way to do this on all platforms.
An example wrapper for Python 3+ would be using format strings:
import datetime
class dWrapper:
def __init__(self, date):
self.date = date
def __format__(self, spec):
caps = False
if '^' in spec:
caps = True
spec = spec.replace('^', '')
out = self.date.strftime(spec)
if caps:
out = out.upper()
return out
def __getattr__(self, key):
return getattr(self.date, key)
def parse(s, d):
return s.format(dWrapper(d))
d = datetime.datetime.now()
print(parse("To Upper doesn't work always {0:%d} of {0:%^B}, year {0:%Y}", d))
Upvotes: -2
Reputation: 183
Just use .upper()
:
print(getDate.strftime("%Y-%B-%d").upper())
Upvotes: 9
Reputation: 363243
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.
Upvotes: 19