Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format doesn't support ordinal indicator formatter #26

Open
brod-ie opened this issue Nov 30, 2015 · 5 comments
Open

format doesn't support ordinal indicator formatter #26

brod-ie opened this issue Nov 30, 2015 · 5 comments

Comments

@brod-ie
Copy link

brod-ie commented Nov 30, 2015

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

Example "3rd Oct"

@yannickl
Copy link
Owner

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.

@brod-ie
Copy link
Author

brod-ie commented Nov 30, 2015

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.

@yannickl
Copy link
Owner

yannickl commented Dec 1, 2015

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.

@brod-ie
Copy link
Author

brod-ie commented Dec 1, 2015

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.

@yannickl
Copy link
Owner

yannickl commented Dec 2, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants