What is this NSDateFormatter style Friday, December 18

I want to know what date format is Friday, December 18th , as well as this standard date format, or I have to do some hard work to get this.

Thanks.

+4
source share
3 answers

I don’t think that th can be done using formatter. 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]; 
+8
source

@Robin NSDateFormatter Help is what helps NSDateFormatter in this case, you really need to work hard to get this format. Just view the link I shared with you.

Good luck

+3
source

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

So here is my comment:

The bad thing with these custom date formats is that if you are not from the United States or another country that uses the same date as the date is incorrect.

Here is how your solution will look on my device: Freitag, Februar 18th
My mom will say th ? What does it mean?

And I would say: "Another developer who never left his country."

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

Because that's what I'm used to. Here's what the date should look like.

please add a check that uses only personalized date formats if the current locale is similar to the custom date format 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 to each component that you want in your dateformat format, and you return a format that uses the locale.

So, if you want to use a weekday, day, and month, you will use something like this:

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

and he will return EEEE, d. MMMM EEEE, d. MMMM for German locale. Or EEEE, MMMM d for our locale. Then you can use this to set a custom dateFormat.

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

I really hate apps that combine date style and German date style.

+3
source

Source: https://habr.com/ru/post/1340311/


All Articles