use*_*028 3 nsdateformatter ios nslocale
我正试图从nsdate对象获取本地化的工作日
+ (NSString *)localizedWeekdayForDate:(NSDate *)date
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:[NSLocale localeWithLocaleIdentifier:language]];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
return formattedDateString;
}
Run Code Online (Sandbox Code Playgroud)
语言字符串总是"en"...即使你的设备语言不是英语......我试过[NSLocale currentLocale]; 以及首选的语言......这也行不通..
有什么建议?
[NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:[NSLocale localeWithLocaleIdentifier:language]]
Run Code Online (Sandbox Code Playgroud)
不设置语言环境,它只返回一个NSString.区域设置需要设置为:
- (void)setLocale:(NSLocale *)locale
Run Code Online (Sandbox Code Playgroud)
例子:
的ObjectiveC
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"fr"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:@"EEEE"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: '%@'", formattedDateString);
Run Code Online (Sandbox Code Playgroud)
NSLog输出:
formattedDateString:'mardi'
斯威夫特3
let date = Date()
let dateFormatter = DateFormatter()
let locale = Locale(identifier:"fr")
dateFormatter.locale = locale
dateFormatter.dateFormat = "EEEE"
let formattedDateString = dateFormatter.string(from:date)
print("formattedDateString: \(formattedDateString)")
Run Code Online (Sandbox Code Playgroud)
输出:
formattedDateString:'mardi'
| 归档时间: |
|
| 查看次数: |
2604 次 |
| 最近记录: |