如何使用NSDateFormatter解析ISO 8601 / RFC 3339时区?

时间:2010-09-04 16:43:44

标签: ios objective-c nsdateformatter rfc3339

我有一个这样的日期字符串(ISO 8601 combined date and time representation或RFC 3339):

  

2010-09-04T19:13:00Z

但是如何配置NSDateFormatter以接受此时区格式?

2 个答案:

答案 0 :(得分:0)

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:'2010-09-04T19:13:00Z'];

答案 1 :(得分:-1)

了解Apple推荐的内容here

// Convert the RFC 3339 date time string to an NSDate.

NSDateFormatter *rfc3339DateFormatter = [[[NSDateFormatter alloc] init] autorelease];

NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];