Question

In dealing with a wide variety of JSON data being sent from various clients date format standardization is a real problem.

I might get any of these: 2013-10-05
2-6-13
Mon, Jul 13 2013

Sometimes there's hours, minutes and seconds as well as time zone information. For each client I've had to manually set up an NSDateFormatter, or if unable to get it parsing I've done substring searching. What I'd really like to do is get all this combined into a single method and somehow determine which type I'm dealing with. Then have some kind of switch statement that handles the parsing for that particular date.

I'm unable to change their formats, but how can I deal with them better?

Was it helpful?

Solution 2

Okay thanks for the comments. What I ended up doing is going to http://waracle.net/iphone-nsdateformatter-date-formatting-table/ and studying VERY CAREFULLY all the NSDateFormatter options. In particular it was the time zone +0000 at the end of RSS posts which was messing up my format conversions. RSS dates match RFC 822, which means you need to use THREE Z's at the end to properly catch it.

I was attempting things like +ZZZZ which were failing on the strings. That's why I was trimming the strings before. What ended up working was this:

@"Wed, 17 Jul 2013 03:23:18 +0000"

Needs formatter of:

@"EEE, dd MMM yyyy HH:mm:ss ZZZ"

After having correctly set up the DateFormatter for this case I was easily able to figure out others for the other formats. It turns out there's only 3 different date formats being used in the projects and I can easily store the right date format string along side each data source and plug that into the date formatter when needed. No more string searching needed and all data is cleanly converting to the correct NSDate.

So the answer is, make a sample project that converts various date formats, plug in the ones you need and use the DateFormatter codes correctly. Chances are your date problems aren't as bad as you think and a better understanding of them can lead to cleaner code.

Using my example below you will see (null) if your format string is incorrect.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        NSArray *dateStrings = @[
             // From data source 1.
            @[@"2012-04-18",@"yyyy-MM-dd"],
            //  From data source 2.
            @[@"2012-04-19 18:29:35",@"yyyy-MM-dd HH:mm:ss"],
            @[@"2011-05-04 11:10:50",@"yyyy-MM-dd HH:mm:ss"],
            // From RSS feeds (pubDate)
            @[@"Wed, 17 Jul 2013 02:24:23 +0000",@"EEE, dd MMM yyyy HH:mm:ss ZZZ"],
            @[@"Tue, 25 Jun 2013 15:04:49 +0000",@"EEE, dd MMM yyyy HH:mm:ss ZZZ"]
                                 ];
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        for(NSArray *datePair in dateStrings){
            [formatter setDateFormat:datePair[1]];
            NSDate *thisDate = [formatter dateFromString:datePair[0]];
            [formatter setDateStyle:NSDateFormatterMediumStyle];
            NSLog(@"%@ = %@",datePair[0],[formatter stringFromDate:thisDate]);
        }

    }
    return 0;
}

OTHER TIPS

This is not exactly the answer to your question but when I'm looking to parse dates without being sure of the format I usually go for NSDataDetector setup for dates only. The detector will often detect dates in multiple languages and format all at once. It's pretty neat.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top