Parse JSON date into NSDate depending user settings

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 ?


You just need to set the correct dateFormat, rest of your code is correct.

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];

The character Z in your dateString is a valid date format defined by ISO8601, it denotes that the date string is in UTC timezone. So you don't need to set the timezone to the formatter. Formatter can recognise the timezone from character Z . So if it is ignored there would be mismatches in timezone unless a timezone is specified to dateFormatter.

If your dateString is not uniform, put a fall back check for dateFormat. Check for converted date to be nil, if nil use the fall back dateFormat.

Also no need to set the calendar to dateFormatter unless there is change in the calendar type of date string and system calendar.


I was trying to do the same thing, but in my case the JSON date also included milliseconds, the date being sourced from MongoDB.

Here is the formatting snippet when you have milliseconds (eg 2014-02-27T09:30:00.000Z )

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSz"];

Please use below code which is working perfect for me.

 NSString* strDate = [jsonObj objectForKey:@"created_at"];
 NSDate *date = [self dateWithJSONString:strDate];

- (NSDate*)dateWithJSONString:(NSString*)dateStr
{
    // Convert string to date object
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    NSDate *date = [dateFormat dateFromString:dateStr];

    // This is for check the output
    // Convert date object to desired output format
    [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; // Here you can change your require output date format EX. @"EEE, MMM d YYYY"
    dateStr = [dateFormat stringFromDate:date];
    NSLog(@"Date -- %@",dateStr);

    return date;
}

Date Format Problem

链接地址: http://www.djcxy.com/p/46662.html

上一篇: 如何将Firebase时间戳转换为日期和时间

下一篇: 根据用户设置将JSON日期解析为NSDate