YLMoment icon indicating copy to clipboard operation
YLMoment copied to clipboard

`format` doesn't support ordinal indicator formatter

Open brod-ie opened this issue 9 years ago • 5 comments

https://en.wikipedia.org/wiki/Ordinal_indicator

Example "3rd Oct"

brod-ie avatar Nov 30 '15 14:11 brod-ie

Hi,

Unfortunately yes. YLMoment format method uses the the NSDate one. So it only supports the same format string (https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW4). It should be great to support this kind of feature but it needs a lot of refactoring I think.

yannickl avatar Nov 30 '15 20:11 yannickl

It depends how tidy a solution you'd be looking to implement. A suggestion from SO using a pretty dirty switch statement:

- (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";
    }
}

You'd then have to use a Regex to search for the special character in the formatter argument.

brod-ie avatar Nov 30 '15 20:11 brod-ie

Yes the dirty solution is simple.

But the formatter one requires some work. If I implement this feature I would conforms to the moment.js one (Do input). It's do-able, but I don't have the time for the moment to think about the best solution. I would use as much as possible the NSDateFormatter and "just" add the missing features.

yannickl avatar Dec 01 '15 20:12 yannickl

Sure I understand. In iOS 9 Apple introduced NSNumberOrdinalStyle as a number style:

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"

Might be useful one day when you come to implement.

brod-ie avatar Dec 01 '15 23:12 brod-ie

It could be useful if I only support iOS 9+. May be it make sense for a new major version of YLMoment.

yannickl avatar Dec 02 '15 09:12 yannickl