【问题标题】:How to parse ISO 8601 date in objective-c [duplicate]如何在objective-c中解析ISO 8601日期[重复]
【发布时间】:2014-02-07 06:29:48
【问题描述】:

我需要从 json 创建一个 NSDate,并以我从未使用过的格式创建日期。我要返回的日期字符串是2014-02-07T03:10:43.824Z。有谁知道我需要用于日期格式化程序的正确日期格式字符串?

【问题讨论】:

标签: objective-c nsdate iso


【解决方案1】:
 NSDateFormatter *dateFormat = [NSDateFormatter new];
 //correcting format to include seconds and decimal place
 NSString* input = @"2014-02-07T03:10:59:434Z";
 dateFormat.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
 // Always use this locale when parsing fixed format date strings
 NSLocale* posix = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
 dateFormat.locale = posix;
 NSDate* output = [dateFormat dateFromString:input];

【讨论】:

  • @Rob,谢谢,我已经进行了编辑
  • 这对我不起作用。对我有用的日期格式字符串是yyyy-MM-dd'T'HH:mm:ss.SSSZ
  • 试试'Z'而不是Z
  • 不,'Z' 会转义 Z 字符,这不是我们想要的。时间戳中的 Z 是表示 UTC 时区的有效字符(请参阅the UTC section here)。这就是the answers on this SO post 使用 Z 而不是“Z”的原因。
  • 答案包含错误,“Z”应该是 Z。
猜你喜欢
  • 2013-05-14
  • 2011-06-17
  • 1970-01-01
  • 2010-11-11
  • 2021-12-15
相关资源
最近更新 更多