MagicalRecord date parsing

ios, magicalrecord, nsdate, nsdateformatter, objective-c

Solution

Let's look at your string:

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

This is:

- four digit year

- hyphen

- zero-padded month

- hyphen

- zero-padded day of month

- 'T' character

- zero-padded hour

- ':' character

- zero-padded minute

- ':' character

- zero-padded second

- 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`.

Problem

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.

Original source