使用自定义格式将字符串转换为NSDate

时间:2012-11-06 13:00:43

标签: iphone ios nsdate nsdateformatter

  

可能重复:
  NSString to NSDate
  iPhone: How to convert “yyyyMMddThhmmss” format string to 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

1 个答案:

答案 0 :(得分: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;

}
相关问题