Question

Possible Duplicate:
Converting mutliple date format into a single format in iPhone

I am trying to parse an RSS and extract an NSString for dates from the content section of the RSS, and convert it to an NSDate. Here is what I have so far:

The RSS content always will list times as When: Date/Time to ending time. I first scan each item in the RSS to get me what is in between the When: and the 'to' part. I then set a NSDateFormatter. Here is where the issue is. Sometimes it is listed as Thu Sep 6, 2013 1:00PM but in other times, the format used is Thu Sep 6, 2013 1pm. So...the issue I need to solve is setting something up so that no matter the format of date, it is able to return something, because now using "EEE MMM dd, yyyy h:mma" is leaving out the instances where the feed just says 1pm.

NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:articleImage];
[scanner scanUpToString:@"When: " intoString:nil]; // Scan all characters before #

NSString *substring = nil;
[scanner scanString:@"When: " intoString:nil]; // Scan the # character
if([scanner scanUpToString:@" to" intoString:&substring]) {
    // If the space immediately followed the #, this will be skipped
    //[substrings addObject:substring];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setDateFormat:@"EEE MMM dd, yyyy h:mma"];
    NSDate *dateFromString = [[NSDate alloc] init];
    // voila!
    dateFromString = [dateFormatter dateFromString:substring];
    self.thetruedate = dateFromString;
    [dateFormatter release];
    NSLog(@"%@",dateFromString);
    [substrings release];
}
Was it helpful?

Solution

Define a method with all possible date formats in your RSS as,

- (NSDate *)dateFromSourceString:(NSString *)sourceString {

    NSDate *convertedDate = nil;
    NSArray *dateFormatterList = [NSArray arrayWithObjects:@"EEE MMM dd, yyyy h:mma", 
                                  @"EEE MMM dd, yyyy ha", nil];//include all possible dateformats here

    //sourceString = @"Thu Sep 6, 2013 1:00PM";

    if (sourceString) {

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        for (NSString *dateFormatterString in dateFormatterList) {

            [dateFormatter setDateFormat:dateFormatterString];
            NSDate *originalDate = [dateFormatter dateFromString:sourceString];

            if (originalDate) {

                convertedDate = originalDate;
                NSLog(@"Converted date is %@", convertedDate);
                break;
            }
        }
       [dateFormatter release]; //release it if it is a non-arc project
    }

    return convertedDate;
}

And then use it as,

NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:articleImage];
[scanner scanUpToString:@"When: " intoString:nil]; // Scan all characters before #

NSString *substring = nil;
[scanner scanString:@"When: " intoString:nil]; // Scan the # character
if([scanner scanUpToString:@" to" intoString:&substring]) {
    // If the space immediately followed the #, this will be skipped
    //[substrings addObject:substring];

    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful

    NSDate *dateFromString = [self dateFromSourceString:substring];
    self.thetruedate = dateFromString;
    NSLog(@"%@",dateFromString);
    [substrings release];
}

dateFromSourceString method will pick date formatters from dateFormatterList and will try to convert to date. If the format is incorrect, it will return nil. This can be used in your case.

OTHER TIPS

You can create all possible types of date format available in your RSS.

Keep on converting by each date formatter one by one, until the converted date is not null. As invalid format will fail to convert and return a null.

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