MagicalRecord date parsing

回眸只為那壹抹淺笑 提交于 2019-12-01 18:58:35

问题


I've got a date in the following format:

2013-05-04T05:07:09+00:00

I'm using MagicalRecord to map the NSDate automatically. As far as I can see the above date format should comply with MagicalRecord's default date format: yyyy-MM-dd'T'HH:mm:ss'Z'.

I have tried with a custom dateFormat entry in the attribute's user info (see this article):

yyyy-MM-ddTHH:mm:ss+Z, yyyy-MM-dd T HH:mm:ss Z, yyyy-MM-dd'T'HH:mm:ss'+'Z

but none of them work in order to have it parse the date properly and it always returns nil regardless of setting a custom dateFormat or using MagicalRecord's default format.


回答1:


Let's look at your string:

2013-05-04T05:07:09+00:00

This is:

  1. four digit year
  2. hyphen
  3. zero-padded month
  4. hyphen
  5. zero-padded day of month
  6. 'T' character
  7. zero-padded hour
  8. ':' character
  9. zero-padded minute
  10. ':' character
  11. zero-padded second
  12. timezone (with direction from GMT and a separating colon)

Thus, according to the date format specifiers documentation, the pattern you'd want is:

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

Also, be sure to use the en_US_POSIX locale with the NSDateFormatter.



来源:https://stackoverflow.com/questions/16377316/magicalrecord-date-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!