I’m trying to format a NSDate to a string with date format of yyyyMMdd, here’s the code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date =data.createdTime;
NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);
the NSLog return me this value
Tue, 24 May 2011 0:05:01 +0800
(null)
Anyone know which part is wrong?
Thanx in advance.
Perhaps
currentArticle.createdTimeis a valid date butdata.createdTimeisnil? In addition, you are performing the-setDateFormat:selector ondateFormat, but it seems like your date formatter isdateFormatter.Try the following code:
If
dateisnil, then bothNSLog()calls will tell you.Edit
Double check that
data.createdTimeis anNSDateinstance. Perhaps it is an instance of another class, such asNSString, whose-descriptionreturns the displayed date. This would explain whyNSLog()“shows” the date, but the formatter is returning nil.