本文介绍了用于特定语言(如西班牙语)的 Nsdateformatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的申请有 2 种语言,一种是英语,另一种是西班牙语.现在我从服务器收到时间戳,我需要以MMM dd,yyyy"格式显示日期.这给了我2017 年 12 月 23 日",但是当我将其转换为西班牙语时,我需要用西班牙语显示月份名称.您能否建议我将西班牙语中的 12 个月名称指定为缩写形式,还是 NSDateFormatter 具有这种类型的选项.

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]];

推荐答案

只需设置语言环境并从模板创建格式(以设置正确的顺序):

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;

这篇关于用于特定语言(如西班牙语)的 Nsdateformatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 04:38