使用NSDateFormatter解析ISO 8601 [英] Parsing ISO 8601 with NSDateFormatter

查看:160
本文介绍了使用NSDateFormatter解析ISO 8601的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我想要解析的字符串,我已经阅读了关于这方面的各种帖子,但仍然没有解决这个问题。 NSDate 物件:

  2012-04-08T12:00:00.00 +02:00 

我理解问题是时区格式。这是我尝试的日期格式字符串:

  yyyy-MM-dd'T'HH:mm:SSZZZZ 

感谢任何帮助。

解决方案

三件事:




  • 使用 - [NSDateFormatter getObjectValue:forString:range:error:]


  • ZZZZ格式字符串对应于GMT + 02:00之类的内容,与那个GMT在那里。


  • Apple的数据格式化指南建议考虑固定格式的Unix函数,非本地日期



I've read through the various posts on SO about this, but have still not solved the problem.

Here's the string that I'm trying to parse into an NSDate object:

2012-04-08T12:00:00.00+02:00

I understand that the problem is the time zone format. Here's the date format string that I'm trying:

yyyy-MM-dd'T'HH:mm:SSZZZZ

Thanks for any help.

解决方案

Three things:

  • Use the -[NSDateFormatter getObjectValue:forString:range:error:] method to learn how much of the string was parsed and what error prevented parsing further.

  • The ZZZZ format string corresponds to something like "GMT+02:00", with that "GMT" in there. You may need to inject that into the string you're parsing for it to work.

  • Apple's Data Formatting Guide advises "Consider Unix Functions for Fixed-Format, Unlocalized Dates".

这篇关于使用NSDateFormatter解析ISO 8601的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆