Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MagicalRecord date parsing

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.

like image 375
runmad Avatar asked Dec 26 '22 04:12

runmad


1 Answers

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.

like image 100
Dave DeLong Avatar answered Jan 11 '23 12:01

Dave DeLong