问题
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