Last active
June 9, 2016 10:30
-
-
Save ozouai/8b6032c50d758561caa0 to your computer and use it in GitHub Desktop.
Determine English Ordinal Suffix from NSDate
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
// Omar Zouai | https://omarzouai.com/ | |
-(NSString*) suffixForDate:(NSDate*) date { | |
NSDateFormatter *monthDayFormatter = [[NSDateFormatter alloc] init]; | |
[monthDayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; | |
[monthDayFormatter setDateFormat:@"d"]; | |
int date_day = [[monthDayFormatter stringFromDate:date] intValue]; | |
NSString *suffix = @"th"; | |
if(date_day <=10 || date_day >=20) { | |
int singleDay = (date_day%10); | |
if(singleDay == 1) suffix = @"st"; | |
if(singleDay == 2) suffix = @"nd"; | |
if(singleDay == 3) suffix = @"rd"; | |
} | |
return suffix; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment