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];
}