Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nsdateformatter for specific language like Spanish

My application is in 2 language one is English and other is Spanish. Now I receive timestamp from the server and I need to show date in "MMM dd, yyyy" formate. this is giving me "Dec 23, 2017" but when I convert it into Spanish then I need to show month name in Spanish. Can you please suggest do I specify 12 month name in Spanish as a short form or NSDateFormatter has this type of option.

  NSDate *date = [dateFormatter dateFromString:[[[webserviceDict valueForKey:@"CurrentWeekEarning"]objectAtIndex:i-1]valueForKey:@"date"]];
                        [dateFormatter setDateFormat:@"MMM dd, yyyy"];
                        strTitle = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:date]];
like image 439
Chandni Avatar asked Sep 05 '25 03:09

Chandni


1 Answers

Just set the locale and create a format from template (to set correct ordering):

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"es"];
[formatter setLocalizedDateFormatFromTemplate:@"yyyyMMMdd"];
NSString *localizedDate = [formatter stringFromDate:[NSDate date]];
NSLog(@"Localized date: %@", localizedDate); // 26 dic 2017

No need to add commas or other separators manually. They are also dependent on language.

The same can be achieved using predefined formats:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"es"];
formatter.timeStyle = NSDateFormatterNoStyle;
formatter.dateStyle = NSDateFormatterMediumStyle;
like image 163
Sulthan Avatar answered Sep 07 '25 18:09

Sulthan