NSDateFormatter 生成 (null) - 为什么?

发布于 2024-12-18 05:55:13 字数 487 浏览 4 评论 0 原文

我知道有很多关于这个主题的帖子,而且我已经全部阅读了。 疯狂地用谷歌搜索,读我的书,但仍然(空)是我得到的唯一东西。 因此,作为我最后的希望,也是唯一的希望,我转向你。

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),经过几个小时后我放弃了。 我不知道发生了什么事。我以为我做到了,但没有。 请帮我。

I know there is a lot of threads on this subject and I have read them all.
Googled like crazy, read my books, but still (null) is the only thing I get.
So as my last hope and only hope I turn to you.

1) publicationDate is a NSString.
At this givven moment is holding this: Sun, 27 Nov 2011 19:46:00 +01:00

I do this to it:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:publicationDate];
NSLog(@"outDate: %@", date);

outDate says (null) and after way to many hours on this I give up.
I have no clue what is going on. I thought I did but no.
Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的背包 2024-12-25 05:55:13

我认为您的日期格式不正确。请尝试这个:

[dateFormat setDateFormat:@"EEE, dd LLL YYYY HH:mm:ss Z"];

I think you date format is incorrect. Please try this:

[dateFormat setDateFormat:@"EEE, dd LLL YYYY HH:mm:ss Z"];
小帐篷 2024-12-25 05:55:13

如果您尝试将此日期从一种格式重新格式化为另一种格式,则需要分两步执行此操作。

首先使用与输入格式匹配的格式掩码转换为日期,然后使用与所需输出格式匹配的格式转换回字符串。

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.

居里长安 2024-12-25 05:55:13
  1. 在该代码中放入 publicationDateNSLog ,问题很可能会被弄清楚。我认为您的代码看起来不错,所以问题很可能出在数据中,即在 publicationDate 中。

  2. 附注:您不需要 YYYY。您需要 yyyy。请参阅http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1 .

  1. Put an NSLog for publicationDate 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. in publicationDate.

  2. Side note: You don't want YYYY. You want yyyy. See http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1.

呆橘 2024-12-25 05:55:13

谢谢大家!你们中的许多人为我指明了正确的方向。
尤其是最后两个人。

我现在分两步进行,并包括格式的更正。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文