How to parse an ISO8601 date [duplicate]

孤人 提交于 2019-12-19 12:23:59

问题


I got string of date regarding this format:

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

How can I convert this into a date object?


回答1:


Try this solution

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);



回答2:


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


来源:https://stackoverflow.com/questions/8676672/how-to-parse-an-iso8601-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!