Tag: «restkit»
Proper Date Parsing with RestKit. From NSString to NSDate
event Jul 2, 2015 at 12:52
A problem came up because dates were being parsed incorrectly through RestKit - the time zone was not taken into account, so in the end all dates were displayed 3 hours later than they should have been.
Inside RestKit there are already several NSDateFormatters for parsing a date. If one fails, the second one is used, and so on. But none of them matched the format in which I was getting dates through the API. To parse dates correctly, you need to add your own NSDateFormatter, and put it first in the list so it is applied first:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss Z"];
[RKObjectMapping alloc];
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:dateFormatter atIndex:0];