convert string to NSDate with custom format [duplicate]

点点圈 提交于 2019-12-04 02:09:03

问题


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:


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;

}


来源:https://stackoverflow.com/questions/13251633/convert-string-to-nsdate-with-custom-format

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