ios - NSDate 月和星期本地化

标签 ios objective-c nsdate

我正在尝试根据手机的语言(而不是区域格式)显示本地化日期(星期几和月份用字母表示)。

我试过了:

// 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/

相关文章:

javascript - 在 ListView 中添加存储在 JSON 数组中的图像 - React Native

ios - OpenWebRTC 需要后端服务器吗?

ios - 如何刷新 UITableViewCell 中的 UIImage?

iphone - 更新应用程序后加载错误的 xib

iphone - 在iPhone上实现警报的最有效方法

core-data - NSPredicate BETWEEN 与 NSDate 原因 -[__NSDate constantValue] : unrecognized selector sent to instance 0x1e7ff0

objective-c - NSDateComponents weekOfYear 返回错误值

ios - 突出显示动态创建的 UIButtons?

ios - Firebase首次打开事件不正确

objective-c - 如何让我的 UIWebView 或 NSURLConnection 告诉网站它是移动浏览器?