yannickl / YLMoment

Parsing, validating, manipulating, and formatting dates easily in Objective-C (API inspired by moment.js)

Home Page:http://yannickl.github.io/YLMoment

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`format` doesn't support ordinal indicator formatter

brod-ie opened this issue · comments

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.

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.

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.

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.

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