无法使用 NSDateFormatter 解析日期
我正在获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要解析 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
据我记忆,我过去也遇到过这个问题。可以尝试以下几件事:
将 RSS 提要中的“T”替换为空格(显然还要更改日期格式字符串)。
您的 RSS 提要已填充数字,而您的格式字符串则没有。尝试一下“yyyy-MM-ddTHH:mm:ss.Z”。根据此链接,“z”应该大写。 http://www.stepcase .com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
从 RSS 源中剥离时区信息并手动解析。我似乎记得这给我带来了问题。
From memory I've had this problem in the past as well. A couple of things to try:
Replace the "T" in your RSS feed with a space (and obviously change the date format string as well).
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/
Strip off the timezone info from the RSS feed and parse this manually. I seem to remember this causing problems for me.