【发布时间】:2015-09-23 10:49:54
【问题描述】:
我正在尝试解决一个臭名昭著的问题,即当用户将定位时间设置为 12 小时而不是默认的 24 小时时,NSDateFormatter 在不需要时返回“am/pm”日期(覆盖格式字符串)
阅读后,似乎最好的解决方案是将 dateFormatter 语言环境设置为 en_US_POSIX(我们将其用于 API 调用,因此需要忽略任何用户偏好)
但是,它似乎被忽略了?
我有一个分类如下:
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [self init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[self setLocale:en_US_POSIX];
return self;
}
@end
然后我打电话:
sharedInstance.dateFormatter = [[NSDateFormatter alloc] initWithSafeLocale];
[sharedInstance.dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
但是,日期仍然打印为
2015-09-28 11:00:00 pm +0000
我做错了什么似乎忽略了 setLocale?
参考:What is the best way to deal with the NSDateFormatter locale "feechur"?
【问题讨论】:
-
你是如何打印这些日期的?
-
我正在使用 NSLog() 进行调试。在应用程序中,我将 NSDate 分配给直接返回到领域对象并保存到 DB
标签: ios objective-c