Display the date, like "May 5th", using pythons strftime?
Question:
Possible Duplicate:
Python: Date Ordinal Output?
In Python time.strftime can produce output like “Thursday May 05” easily enough, but I would like to generate a string like “Thursday May 5th” (notice the additional “th” on the date). What is the best way to do this?
Answers:
You cannot. The time.strftime
function and the datetime.datetime.strftime
method both (usually) use the platform C library’s strftime
function, and it (usually) does not offer that format. You would need to use a third-party library, like dateutil.
from time import strftime
print strftime('%A %B %dth')
EDIT:
Correcting after having seen the answers of gurus:
from time import strftime
def special_strftime(dic = {'01':'st','21':'st','31':'st',
'02':'nd','22':'nd',
'03':'rd','23':'rd'}):
x = strftime('%A %B %d')
return x + dic.get(x[-2:],'th')
print special_strftime()
.
EDIT 2
Also:
from time import strftime
def special_strftime(dic = {'1':'st','2':'nd','3':'rd'}):
x = strftime('%A %B %d')
return x + ('th' if x[-2:] in ('11','12','13')
else dic.get(x[-1],'th')
print special_strftime()
.
EDIT 3
Finally, it can be simplified:
from time import strftime
def special_strftime(dic = {'1':'st','2':'nd','3':'rd'}):
x = strftime('%A %B %d')
return x + ('th' if x[-2]=='1' else dic.get(x[-1],'th')
print special_strftime()
strftime
doesn’t allow you to format a date with a suffix.
Here’s a way to get the correct suffix:
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
Update:
Combining a more compact solution based on Jochen’s comment with gsteff’s answer:
from datetime import datetime as dt
def suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))
print custom_strftime('%B {S}, %Y', dt.now())
Gives:
May 5th, 2011
This seems to add the appropriate suffix, and remove the ugly leading zeroes in the day number:
#!/usr/bin/python
import time
day_endings = {
1: 'st',
2: 'nd',
3: 'rd',
21: 'st',
22: 'nd',
23: 'rd',
31: 'st'
}
def custom_strftime(format, t):
return time.strftime(format, t).replace('{TH}', str(t[2]) + day_endings.get(t[2], 'th'))
print custom_strftime('%B {TH}, %Y', time.localtime())
"%s%s"%(day, 'trnshddt'[0xc0006c000000006c>>2*day&3::4])
But seriously, this is locale specific, so you should be doing it during internationalisation
Possible Duplicate:
Python: Date Ordinal Output?
In Python time.strftime can produce output like “Thursday May 05” easily enough, but I would like to generate a string like “Thursday May 5th” (notice the additional “th” on the date). What is the best way to do this?
You cannot. The time.strftime
function and the datetime.datetime.strftime
method both (usually) use the platform C library’s strftime
function, and it (usually) does not offer that format. You would need to use a third-party library, like dateutil.
from time import strftime
print strftime('%A %B %dth')
EDIT:
Correcting after having seen the answers of gurus:
from time import strftime
def special_strftime(dic = {'01':'st','21':'st','31':'st',
'02':'nd','22':'nd',
'03':'rd','23':'rd'}):
x = strftime('%A %B %d')
return x + dic.get(x[-2:],'th')
print special_strftime()
.
EDIT 2
Also:
from time import strftime
def special_strftime(dic = {'1':'st','2':'nd','3':'rd'}):
x = strftime('%A %B %d')
return x + ('th' if x[-2:] in ('11','12','13')
else dic.get(x[-1],'th')
print special_strftime()
.
EDIT 3
Finally, it can be simplified:
from time import strftime
def special_strftime(dic = {'1':'st','2':'nd','3':'rd'}):
x = strftime('%A %B %d')
return x + ('th' if x[-2]=='1' else dic.get(x[-1],'th')
print special_strftime()
strftime
doesn’t allow you to format a date with a suffix.
Here’s a way to get the correct suffix:
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
Update:
Combining a more compact solution based on Jochen’s comment with gsteff’s answer:
from datetime import datetime as dt
def suffix(d):
return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')
def custom_strftime(format, t):
return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))
print custom_strftime('%B {S}, %Y', dt.now())
Gives:
May 5th, 2011
This seems to add the appropriate suffix, and remove the ugly leading zeroes in the day number:
#!/usr/bin/python
import time
day_endings = {
1: 'st',
2: 'nd',
3: 'rd',
21: 'st',
22: 'nd',
23: 'rd',
31: 'st'
}
def custom_strftime(format, t):
return time.strftime(format, t).replace('{TH}', str(t[2]) + day_endings.get(t[2], 'th'))
print custom_strftime('%B {TH}, %Y', time.localtime())
"%s%s"%(day, 'trnshddt'[0xc0006c000000006c>>2*day&3::4])
But seriously, this is locale specific, so you should be doing it during internationalisation