What setDateFormat option for NSDateFormatter do I use to get a month-day's ordinal suffix?
e.g. the snippet below currently produces:
3:11 PM Saturday August 15
What must I change to get:
3:11 PM Saturday August 15th
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[dateFormatter setDateFormat:@"h:mm a EEEE MMMM d"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"%@", dateString);
In PHP, I'd use this for the case above:<?php echo date('h:m A l F jS') ?>
Is there an NSDateFormatter equivalent to the S option in the PHP formatting string?
None of these answers were as aesthetically pleasing as what I'm using, so I thought I would share:
Swift 3:
func daySuffix(from date: Date) -> String {
let calendar = Calendar.current
let dayOfMonth = calendar.component(.day, from: date)
switch dayOfMonth {
case 1, 21, 31: return "st"
case 2, 22: return "nd"
case 3, 23: return "rd"
default: return "th"
}
}
Objective-C:
- (NSString *)daySuffixForDate:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger dayOfMonth = [calendar component:NSCalendarUnitDay fromDate:date];
switch (dayOfMonth) {
case 1:
case 21:
case 31: return @"st";
case 2:
case 22: return @"nd";
case 3:
case 23: return @"rd";
default: return @"th";
}
}
Obviously, this only works for English.
NSDate *date = [NSDate date];
NSDateFormatter *prefixDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[prefixDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[prefixDateFormatter setDateFormat:@"h:mm a EEEE MMMM d"];
NSString *prefixDateString = [prefixDateFormatter stringFromDate:date];
NSDateFormatter *monthDayFormatter = [[[NSDateFormatter alloc] init] autorelease];
[monthDayFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[monthDayFormatter setDateFormat:@"d"];
int date_day = [[monthDayFormatter stringFromDate:date] intValue];
NSString *suffix_string = @"|st|nd|rd|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|th|st|nd|rd|th|th|th|th|th|th|th|st";
NSArray *suffixes = [suffix_string componentsSeparatedByString: @"|"];
NSString *suffix = [suffixes objectAtIndex:date_day];
NSString *dateString = [prefixDateString stringByAppendingString:suffix];
NSLog(@"%@", dateString);
This is easily done as of iOS9
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterOrdinalStyle;
NSArray<NSNumber *> *numbers = @[@1, @2, @3, @4, @5];
for (NSNumber *number in numbers) {
NSLog(@"%@", [formatter stringFromNumber:number]);
}
// "1st", "2nd", "3rd", "4th", "5th"
Taken from NSHipster
Swift 2.2:
let numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = .OrdinalStyle
let numbers: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers {
print(numberFormatter.stringFromNumber(number)!)
}
Here's another implementation for a method to generate the suffix. The suffixes it produces are only valid in English and may not be correct in other languages:
- (NSString *)suffixForDayInDate:(NSDate *)date
{
NSInteger day = [[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] components:NSDayCalendarUnit fromDate:date] day];
if (day >= 11 && day <= 13) {
return @"th";
} else if (day % 10 == 1) {
return @"st";
} else if (day % 10 == 2) {
return @"nd";
} else if (day % 10 == 3) {
return @"rd";
} else {
return @"th";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With