How do I display the date, like "Aug 5th", using Python's strftime?
Answers
Answered by
0
It is not possible to get a suffix like st, nd, rd and th using the strftime function. The strftime function doesn't have a directive that supports this formatting. You can create your own function to figure out the suffix and add it to the formatting string you provide. For example,
from datetime import datetime
now = datetime.now()
def suffix(day):
suffix = ""
if 4 <= day <= 20 or 24 <= day <= 30:
suffix = "th"
else:
suffix = ["st", "nd", "rd"][day % 10 - 1]
return suffix
my_date = now.strftime("%b %d" + suffix(now.day))
print(my_date)
Similar questions
Social Sciences,
6 months ago
English,
6 months ago
Math,
6 months ago
Computer Science,
11 months ago
Math,
1 year ago