如何解析ISO8601日期

时间:2011-12-30 06:03:51

标签: iphone objective-c ios date iso8601

我收到了关于这种格式的日期字符串:

2011-12-29T09:09:06-0500

如何将其转换为日期对象?

2 个答案:

答案 0 :(得分:3)

试试这个解决方案

NSString *dateString = @"2011-12-29T09:09:06-0500";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDate *dateFromString = [dateFormatter dateFromString:dateString];    
[dateFormatter release];
NSLog(@" dateFromString %@",dateFromString);

答案 1 :(得分:1)

NSString *date = [[NSDate date] description];

[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];

NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];