Skip to content

Instantly share code, notes, and snippets.

@ozouai
Last active June 9, 2016 10:30
Show Gist options
  • Save ozouai/8b6032c50d758561caa0 to your computer and use it in GitHub Desktop.
Save ozouai/8b6032c50d758561caa0 to your computer and use it in GitHub Desktop.
Determine English Ordinal Suffix from NSDate
// 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