Skip to content

Instantly share code, notes, and snippets.

@makmac213
Last active December 16, 2015 21:39
Show Gist options
  • Save makmac213/5501575 to your computer and use it in GitHub Desktop.
Save makmac213/5501575 to your computer and use it in GitHub Desktop.
returns the date with ordinal suffix
# Mark Allan B. Meriales
def ordinal_date_suffix(dt):
suffix = ''
num_date = int(dt.strftime('%d'))
if (num_date >= 4 and num_date <= 20) or (num_date >= 24 and num_date <= 30):
suffix = 'th'
else:
suffix = ['st','nd','rd'][(num_date % 10) - 1]
return "%s%s" % (num_date, suffix)
def ordinal_number_suffix(n):
suffix = ''
if (n%10) not in [1,2,3]:
suffix = 'th'
else:
suffix = ['st','nd','rd'][(n % 10) - 1]
return "%s%s" % (n, suffix)
@makmac213
Copy link
Author

>>> from datetime import date, timedelta

>>> for i in range(1, 32):
>>>    dt = date.today() + timedelta(days=i)
>>>    print ordinal_date_suffix(dt)

11th
12th
13th
14th
15th
16th
17th
18th
19th
20th
21st
22nd
23rd
24th
25th
26th
27th
28th
29th
30th
31st
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment