1

可能重复:
NSString 到 NSDate
iPhone:如何将“yyyyMMddThhmmss”格式字符串转换为 NSDate?

如何将此格式转换为 NSDate ?

我的字符串日期格式是:(yyyy-m-d'T'H:m:ss间隔之间没有空格)。

更新:

2012-11-06T12:17:22.947 would be the string that I'd like to convert to NSDate
4

1 回答 1

6

只需将此方法粘贴到您的 .m 文件中,然后使用您的 stringDate 调用此方法..

-(NSDate *)convertStringToDate:(NSString *) date {

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
nowDate = [formatter dateFromString:date];
// NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
return nowDate;

}
于 2012-11-06T13:02:33.697 回答