convert string to NSDate with custom format [dupli

2019-02-20 14:22发布

Possible Duplicate:
NSString to NSDate
iPhone: How to convert “yyyyMMddThhmmss” format string to NSDate?

How do I convert this format to NSDate ?

My string date format is: yyyy-m-d'T'H:m:ss (no space between intervals).

Update:

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

1条回答
放荡不羁爱自由
2楼-- · 2019-02-20 14:44

just paste this method in your .m file and then call this method with your 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;

}
查看更多
登录 后发表回答