NSDateFormatter 错误解析日期
我正在从表示日期的文本中提取字符串。它们看起来像这样:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请问,
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.
“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