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.