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


  2010-09-04T19:13:00Z


但是,如何配置NSDateFormatter接受这种时区格式?

最佳答案

看看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];

关于ios - 如何使用NSDateFormatter解析ISO 8601/RFC 3339时区?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3643147/

10-09 01:45