I have a problem for converting a brut JSON string date:
"created_at" = "2012-12-22T21:39:22Z";
into an NSDate.
Here is my current category for that:
- (NSDate*)dateWithJSONString
{
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
NSDate *date = [[NSDate alloc] init];
date = [dateFormatter dateFromString:self];
return date;
}
The problem is this works for 95% of my cases, but if some users use AM/PM instead of 24H in there date and time settings combined with custom international settings, the date is not created from the json string.
I read this discussion but I can't find the good way to work with all my international users and settings.
I added this for example:
[dateFormatter setLocale:[NSLocale currentLocale]];
but it changed nothing. The NSDate is not created if I set my iPhone with AM/PM instead of 24H.
AM/PM NSDate schema: 2013-01-21 07:12:43 AM +0000
24H NSDate schema: 2013-01-21 07:12:43 +0000
What did I miss ? Is there something to update in the date format ?