NSDate dateFromString deprecated?

社会主义新天地 提交于 2019-11-30 15:51:56

NSDateFormatter is the intended way for you to get an NSDate from an NSString.

The most basic usage is something like this:

NSString *dateString = @"3-Aug-10";

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.dateFormat = @"d-MMM-yy";

NSDate *date = [dateFormatter dateFromString:dateString];

I had a same problem. I changed the dateFormat from @"YYYY/M/d H:m:s" to @"YYYY/MM/dd HH:mm:ss" as follows. Then it works on my iPhone 4. (Xcode 4.5 for iOS 6.)


NSDateFormatter *dateFormatter1;
NSString *dateStr1=@"";
NSDate *gantanDate1;

dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setLocale:[NSLocale systemLocale]]; 
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]]; 
dateFormatter1.dateFormat=@"YYYY/MM/dd HH:mm:ss";
dateStr1=@"2012/1/1 00:00:00"];
//  in his case,   dateStr1=pickerDate;  
gantanDate1 = [dateFormatter1 dateFromString:dateStr1];

Hi Silber everyone says the same thing to convert the string date to date object.Try this i think you have used two date formatters in two places where you are saving date to string and getting date from string.right try to use the same date formatters in both places. It will solve your problem.

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