-->

Which is this NSDateFormatter style Friday, Decemb

2020-07-17 07:12发布

问题:

I want to know which is this date format Friday, December 18th and also is this standard date format or i have to some hard work to get this.

Thanks.

回答1:

I don't think the th is possible to achieve via formatters. Although you can do it like this:

NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"EEEE, MMMM d"];
    NSString *dateString = [dateFormat stringFromDate:today];
    NSCalendar *cal = [NSCalendar currentCalendar];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *dtComp = [cal components:unitFlags fromDate:today];
    switch ([dtComp day]) {
        case 1:
        case 31:
        case 21:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ist",[dtComp day]]];
            break;
        case 2:
        case 22:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ind",[dtComp day]]];
                break;
        case 3:
        case 23:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ird",[dtComp day]]];
            break;
        default:
            dateString = [dateString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%i",[dtComp day]] withString:[NSString stringWithFormat:@"%ith",[dtComp day]]];
            break;
    }

    [dateFormat release];


回答2:

@Robin Help with NSDateFormatter is something which will help u a lot in this case of NSDateFormatter, you really have to work hard to get this format,Just see the link i had shared with you.

Good Luck!



回答3:

If it would be possible to post long comments in a readable way I would comment on Madhups answer.

So here is my comment:

The bad thing with those custom date formats is that if you are not from the united states or another country that uses the same dateformat the date is wrong.

This is how your solution would look on my device: Freitag, Februar 18th
My mother would say th? What does that mean?

And I would say "Another developer that has never left his country."

Please I want my date like this: Freitag, 18. Februar

Because this is what I'm used to. This is how a date should look like.

so please add a check that only uses custom date formatters if the current locale is similar to the custom dateformat you are using.


In iOS4 Apple introduced a really cool method.

+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale

you pass this method every component you want in your dateformat and you get back a format that uses the locale.

So if you want to use the weekday, the day and the month you would use something like this:

NSString *localizedDateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE MMMM d" options:0 locale:[NSLocale currentLocale]];

and it would return EEEE, d. MMMM for a german locale. Or EEEE, MMMM d for the us locale. You can then use that to set a custom dateFormat.

That's the whole point of NSDateFormatter. Localized dates. If you use [dateFormatter setDateFormat:@"some hard coded date format"] to show dates to the user you are doing it wrong in most cases.

I really hate apps that mix US date style and german date style.