NSDateFormatter 生成 (null) - 为什么?
我知道有很多关于这个主题的帖子,而且我已经全部阅读了。 疯狂地用谷歌搜索,读我的书,但仍然(空)是我得到的唯一东西。 因此,作为我最后的希望,也是唯一的希望,我转向你。
1)publicationDate是一个NSString。 在这个给定的时刻持有这个:Sun, 27 Nov 2011 19:46:00 +01:00
我对其执行以下操作:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:publicationDate];
NSLog(@"outDate: %@", date);
outDate 说(null),经过几个小时后我放弃了。 我不知道发生了什么事。我以为我做到了,但没有。 请帮我。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您的日期格式不正确。请尝试这个:
I think you date format is incorrect. Please try this:
如果您尝试将此日期从一种格式重新格式化为另一种格式,则需要分两步执行此操作。
首先使用与输入格式匹配的格式掩码转换为日期,然后使用与所需输出格式匹配的格式转换回字符串。
If you are trying to reformat this date from one format to another format, then you need to do this in two steps.
First convert to a date using a format mask that matches the input format, then convert back to a string using a format that matches the output format you desire.
在该代码中放入
publicationDate
的NSLog
,问题很可能会被弄清楚。我认为您的代码看起来不错,所以问题很可能出在数据中,即在publicationDate
中。附注:您不需要
YYYY
。您需要yyyy
。请参阅http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1 .Put an
NSLog
forpublicationDate
in that code and there is a very good chance the problem will be clear. You're code looks OK, I think, so chances are the problem is in the data, i.e. inpublicationDate
.Side note: You don't want
YYYY
. You wantyyyy
. See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1.谢谢大家!你们中的许多人为我指明了正确的方向。
尤其是最后两个人。
我现在分两步进行,并包括格式的更正。
Thank you all! Many of you have pointed me in the right direction.
Especially the last two people.
I have made in two steps now and included the correction of the format.