NSDateFormatter 错误解析日期

发布于 2024-09-14 04:21:43 字数 471 浏览 5 评论 0原文

我正在从表示日期的文本中提取字符串。它们看起来像这样:

Monday August 16, 2010 05:28 AM EST

我正在尝试使用 NSDateFormatter 来解析它们。我已将其格式设置为:

[dateFormatter setDateFormat:@"EEEE MMMM d, YYYY h:mm a z"];

但是,这不起作用。对于我上面给出的示例,如果我将字符串转换为日期,然后将该日期转换为字符串,则日期格式化程序将返回以下内容:

Monday December 28, 2009 5:28 AM EST

我在做什么错误的?

编辑:使用格式 [dateFormatter setDateFormat:@"EEEE MMMM d, yh:mm a z"]; 时似乎有效。奇怪的...

I'm extracting strings from a text that represent dates. They look like this:

Monday August 16, 2010 05:28 AM EST

I'm trying to parse them with a NSDateFormatter. I've set its format to:

[dateFormatter setDateFormat:@"EEEE MMMM d, YYYY h:mm a z"];

However, this doesn't work. For the example I gave above, if I convert the string to a date and then that date to a string, the date formatter returns this:

Monday December 28, 2009 5:28 AM EST

What am I doing wrong?

EDIT: It seems to work when using the format [dateFormatter setDateFormat:@"EEEE MMMM d, y h:mm a z"];. Strange...

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

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

发布评论

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

评论(2

夜吻♂芭芘 2024-09-21 04:21:43

请问,

NSDateFormatter 使用 Unicode 标准来格式化日期。正如您所发现的,“y”有效,但“YYYY”无效。如果你看到规范(在苹果文档中引用了几层,所以不容易找到),你会看到“Y”有这样的注释:“Year(of“Week of Year”),用于 ISO 年-周日历可能与日历年不同。”

这是规范的链接。

我知道您已经修复了你自己做吧,但这可能有助于解释为什么“Y”不适合你。

Will,

NSDateFormatter uses the Unicode standard for formatting dates. As you've found, "y" works, but "YYYY" doesn't. If you see the spec (referenced several layers deep in the Apple documentation, so not easy to find) you'll see that "Y" has this note: "Year (of "Week of Year"), used in ISO year-week calendar. May differ from calendar year."

Here is the link to the specification.

I know you've fixed it yourself, but this may help explain why "Y" didn't work for you.

失去的东西太少 2024-09-21 04:21:43

“y”或“yyyy”都可以。 “YYYY”不起作用。
检查以下链接了解详细信息。

situee.blogspot.com:NSDateFormatter setDateFormat YYYY 返回错误的年份

"y" or "yyyy" is ok. "YYYY" doesn't work.
Check the following link for detail.

situee.blogspot.com: NSDateFormatter setDateFormat YYYY return wrong year

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