我正在尝试根据手机的语言(而不是区域格式)显示本地化日期(星期几和月份用字母表示)。
我试过了:
// Date formatter
NSDateFormatter* df = [[NSDateFormatter alloc] init];
NSLocale* locale = [NSLocale currentLocale];
NSLog(@"locale : %@", locale.localeIdentifier);
[df setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"EEEE dd MMMM" options:0 locale:[NSLocale currentLocale]]];
但是当我更改手机的语言时,它没有任何变化,它仍然以法语显示。我怎样才能让它发挥作用?
最佳答案
试试这个;
首先我们获取设备语言,然后我们设置 NSDateFormatter
的语言环境
用那种语言。
NSString * deviceLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSDateFormatter * dateFormatter = [NSDateFormatter new];
NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:deviceLanguage];
[dateFormatter setDateFormat:@"EEEE dd MMMM"];
[dateFormatter setLocale:locale];
NSString * dateString = [dateFormatter stringFromDate:[NSDate date]];
NSLog(@"%@", dateString);
关于ios - NSDate 月和星期本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18783367/