问题
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 NSLocale
s to the formatter and describing your results.
来源:https://stackoverflow.com/questions/19453436/nsdateformatter-strings-in-ios-7