本文介绍了使用自定义格式将字符串转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将此格式转换为NSDate?
How do I convert this format to NSDate ?
我的字符串日期格式为: yyyy-m-d'T'H:m:ss
(间隔之间没有空格)。
My string date format is: yyyy-m-d'T'H:m:ss
(no space between intervals).
更新:
2012-11-06T12:17:22.947 would be the string that I'd like to convert to NSDate
推荐答案
只需将此方法粘贴到.m文件中,然后使用stringDate调用此方法..
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;
}
这篇关于使用自定义格式将字符串转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!