我有一个这样的日期字符串(ISO 8601 组合日期和时间表示或 RFC 3339):
2010-09-04T19:13:00Z
但是如何配置 NSDateFormatter 以接受这种时区格式?
[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'];
看看这里有什么苹果推荐:
// 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];