NSDateFormatter Strings in iOS 7

烈酒焚心 提交于 2019-12-10 16:45:28

问题


Attempting to format a date in an iOS 7 and getting some unexpected results for day of the week. Apple's documentation does not list patterns for iOS 7, but the patterns provided for iOS 6 are here. Looking at the day of the week section

Day of week - Use one through three letters for the short day, or four for the full name, or five for the narrow name.

However I'm unable to get the 4 letter full name formatter string to work.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

prints "Wed"

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

also prints "Wed", should print "Wednesday"

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEEE"];
NSLog(@"%@", [formatter stringFromDate:date]);

prints "W"

Is there a new set of formatting strings that should be used for iOS 7 or am I doing something incorrectly?


回答1:


As others have noticed, it looks like a locale issue.

Try to force a known locale like follows

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
// or
NSLocale *locale = [NSLocale currentLocale];
[formatter setLocale:locale];
[formatter setDateFormat:@"EEEE"];
NSLog(@"%@", [formatter stringFromDate:date]);



回答2:


There was a new Unicode standard (released Sept 18, 2013), but that doesn't change the EEEE format.

I'd guess this is locale dependent. iOS might be trying to be clever, shortening the name because it better matches the default locale. Try adding a few different NSLocales to the formatter and describing your results.



来源:https://stackoverflow.com/questions/19453436/nsdateformatter-strings-in-ios-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!