NSDateFormatter 返回 null
我正在尝试将 NSDate 格式化为日期格式为 yyyyMMdd 的字符串,这是
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date =data.createdTime;
NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);
NSLog 返回我这个值的
Tue, 24 May 2011 0:05:01 +0800
(null)
代码有人知道哪一部分是错误的吗?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许
currentArticle.createdTime
是一个有效的日期,但data.createdTime
是nil
?此外,您正在对dateFormat
执行-setDateFormat:
选择器,但您的日期格式化程序似乎是dateFormatter
。尝试以下代码:
如果
date
为nil
,则两个NSLog()
调用都会告诉您。编辑
仔细检查
data.createdTime
是一个NSDate
实例。也许它是另一个类的实例,例如NSString
,其-description
返回显示的日期。这可以解释为什么 NSLog()“显示”日期,但格式化程序返回 nil。Perhaps
currentArticle.createdTime
is a valid date butdata.createdTime
isnil
? In addition, you are performing the-setDateFormat:
selector ondateFormat
, but it seems like your date formatter isdateFormatter
.Try the following code:
If
date
isnil
, then bothNSLog()
calls will tell you.Edit
Double check that
data.createdTime
is anNSDate
instance. Perhaps it is an instance of another class, such asNSString
, whose-description
returns the displayed date. This would explain whyNSLog()
“shows” the date, but the formatter is returning nil.应该是:
如果它仍然不起作用,那么
data.createdTime;
出了问题,因为我运行了这段代码并收到了预期的输出:Should be:
If it still isn't working then something is wrong with
data.createdTime;
because I ran this code and recieved the expected output: