我正在使用下面的代码。
NSString * weekdayString = [[self date] descriptionWithLocale:@"%A"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
dateString = [NSString stringWithFormat:@"%@,%@",weekdayString,[dateFormatter stringFromDate:[self date]]];
NSArray * components = [dateString componentsSeparatedByString:@","];
dateString = [NSString stringWithFormat:@"%@,%@",[components objectAtIndex:0],[components objectAtIndex:1]];
它在模拟器和 ios 6.0 中运行良好,但在设备 (iPad) 中的 ios 5.1 中未按预期显示。
最佳答案
NSDate -descriptionWithLocale
记录为采用 NSLocale
;您正在传递一个 NSString
,因此依赖于未记录的行为。
还不清楚您试图通过拆分成组件来实现什么——大概您已经假设 NSDateFormatterShortStyle 中始终有一个逗号?文档也不能保证这一点。
您可能想要做的只是设置一个自定义日期格式字符串。从您的帖子标题来看,您可能想要 EEEE, MMMM dd
。所以例如
// see QA1480 re: en_US_POSIX
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:usLocale];
[dateFormatter setDateFormat:@"EEEE, MMMM dd"];
dateString = [dateFormatter stringFromDate:[self date]];
QA1480解决了一个问题,该问题可能导致 NSDateFormatter
静默地在内部调整您的日期格式化程序。
关于ios - 日期格式如 "Monday, December 10"在 ios 5.1 中错误显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14702192/