MagicalRecord date parsing

2019-02-25 05:17发布

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条回答
【Aperson】
2楼-- · 2019-02-25 05:46

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.

查看更多
登录 后发表回答