- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试解决臭名昭著的问题,即当用户将定位时间设置为 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 description]
,它将使用用户的默认语言环境。
来自docs (descriptionWithLocale
):
If you pass
nil
,NSDate
formats the date in the same way as thedescription
property.On OS X v10.4 and earlier, this parameter was an
NSDictionary
object. If you pass in anNSDictionary
object on OS X v10.5,NSDate
uses the default user locale—the same as if you passed in[NSLocale
.
currentLocale]
相反,您应该使用日期格式化程序来打印格式化字符串。
关于ios - NSDateFormatter - setLocale 被忽略了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32737588/
我在 iOS 6 上使用 xcode 4.5(4G182)。NSDateFormatter 在 iOS 6 中显示错误的年份,如何解决? NSDateFormatter *dateFormatter
我在 iOS 6 上使用 xcode 4.5(4G182)。NSDateFormatter 在 iOS 6 中显示错误的年份,如何解决? NSDateFormatter *dateFormatter
我正在尝试创建一个 TableViewCell,就像 iPhone 上的 Notes 应用程序中的那样。标题在左侧,时间在右侧,紧挨着披露者指示符。 看起来像那样的 tableView。我知道我必须使
我有一个用于“从”和“到”字段的日期选择器,我想要减法的结果。例如:toValue-fromValue,结果以小时为单位。如何做到这一点? 最佳答案 可以使用timeIntervalSinceDate
我简单地尝试重新格式化日期: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSDate *date = [[NSDat
在 Instrument 的帮助下,我发现以下代码段存在内存泄漏。仪器显示 NSDateFormatter 此处泄漏内存。 - (NSDate*) dateSelected{ if(selectedD
当用户未使用公历作为 iPhone 的默认日历时,我在使用 NSDateFormatter 时遇到问题。 NSString *testString = @"2011-01-14"; NSDateFor
以下日期/时间的 NSDateFormatter 字符串是什么:02/22/2011 10:43:00 AM 基本上我想要的是读取字符串02/22/2011 19:00:23 PM,将其转换为NSDa
我正在尝试创建一个格式化程序,将显示的日期格式转换为 NSDate 对象: NSString *dateStr = @"2010-06-21T19:00:00-05:00"; NSDateFormat
我有一个带有 NSDateFormatter 的 NSTextField。格式化程序接受“mm/dd/yy”。 可以自动补全日期吗?因此,用户可以输入“mm”,格式化程序将完成当前月份和年份。 最佳答
我正在从表示日期的文本中提取字符串。它们看起来像这样: Monday August 16, 2010 05:28 AM EST 我正在尝试使用 NSDateFormatter 来解析它们。我已将其格式
我有一个应用于 NSTextFieldCell 的 10.4 样式日期格式化程序。我调用 setTwoDigitStartDate: (尝试使用纪元日期和“引用”日期),但是当我输入带有两位数年份的日
在打开 doesRelativeDateFormatting 的情况下使用 NSDateFormatter 时,所有相对格式似乎都相对于当前系统时间。我想使用相对格式设置日期,例如“今天”或“明天”,
我想从日期获取 NSString,日期格式如下: MMEEEY ww 问题出在“ww”,即一年中的第几周。我必须将此格式与从后台获取的另一种格式进行比较。但后台的第一个工作日是星期一,而 iOS 的第
我的日期格式化程序返回nil表示一个特定日期:“1998年3月20日”。 + (NSDate*)dateFromString:(NSString*)date { NSDateFormatter
我知道这篇文章可能一式三份,但是到目前为止,关于现有线程的所有答案都没有帮助我。 我试图从字符串中获取NSDate值,但结果一直是nil。这是我的代码 NSString * dateString =
Crashlytics服务报告一些崩溃(大约30个崩溃/ 18个用户进行1000个 session ) 这是我的代码: var brutDate: String = "" brutDate () (
我正在尝试解析这个日期时间戳 开始串: Wed, 11 Sep 2013 08:51:41 EEST 这个问题只有 EEST , 我试过 z 或 zzz 或 V,什么也没发生。日期格式化程序始终为 N
这个问题在这里已经有了答案: NSDate Format outputting wrong date (5 个回答) 8年前关闭。 我正在使用下面的代码,但它给了我提前一天 date 。 示例 :将输
使用设备上用户的currentLocale,我想要一种与NSDateFormatterFullStyle生成的日期格式完全相同的日期格式减去年份: [dateFormatter setDateStyl
我是一名优秀的程序员,十分优秀!