无法使用 NSDateFormatter 解析日期

发布于 2024-08-31 17:59:56 字数 343 浏览 1 评论 0原文

我正在获取 RSS,其中收到以下日期戳:

2010-05-10T06:11:14.000Z

现在我正在使用 NSDateFormatter 来解析此日期时间戳。

[parseFormatter setDateFormat:@"yyyy-MM-dTH:m:s.z"];

但如果只删除时间戳部分,它就无法正常工作 [parseFormatter setDateFormat:@"yyyy-MM-d"]; 但如果我添加其余的内容,它会返回 nil 。

有什么想法吗?

提前致谢....

I am fetching a RSS, in which i receive the following Date stamp:

2010-05-10T06:11:14.000Z

Now i am using NSDateFormatter to parse this datetime stamp.

[parseFormatter setDateFormat:@"yyyy-MM-dTH:m:s.z"];

But its not working fine if just remove the time stamp part it works for the date [parseFormatter setDateFormat:@"yyyy-MM-d"]; But if i add the rest of the stuff it returns nil.

Any idea ?

Thanks in Advance....

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

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

发布评论

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

评论(2

别忘他 2024-09-07 17:59:56

您需要解析 d、H、m、s 的零填充值

您需要将文字 T 转义为 'T'

您需要解析秒的小数部分使用 SSS

您可以接受带有 'Z' 的文字 Z 或使用大写 Z 来尝试解析时区,但 RSS 使用单独的标准。

@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

日期格式模式

You need to parse zero padded values for d, H, m, s

You need to escape the literal T as 'T'

You need to parse the fractional part of the seconds with SSS

You can accept a literal Z with 'Z' or use uppercase Z to try and parse the timezone but RSS uses separate standard.

@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

Date Format Patterns

萌能量女王 2024-09-07 17:59:56

据我记忆,我过去也遇到过这个问题。可以尝试以下几件事:

  1. 将 RSS 提要中的“T”替换为空格(显然还要更改日期格式字符串)。

  2. 您的 RSS 提要已填充数字,而您的格式字符串则没有。尝试一下“yyyy-MM-ddTHH:mm:ss.Z”。根据此链接,“z”应该大写。 http://www.stepcase .com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/

  3. 从 RSS 源中剥离时区信息并手动解析。我似乎记得这给我带来了问题。

From memory I've had this problem in the past as well. A couple of things to try:

  1. Replace the "T" in your RSS feed with a space (and obviously change the date format string as well).

  2. Your RSS feed has padded digits, where as your format string does not. Give this a try "yyyy-MM-ddTHH:mm:ss.Z". According to this link, that "z" should be capilized. http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/

  3. Strip off the timezone info from the RSS feed and parse this manually. I seem to remember this causing problems for me.

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