Last active
December 16, 2015 21:39
-
-
Save makmac213/5501575 to your computer and use it in GitHub Desktop.
returns the date with ordinal suffix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
>>> 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