开发者

NSDateFormatter with RSS pubDate

I have some problems with converting a rss pubdate string into a NSDate object.

<pubDate>Fri, 09 Sep 2011 15:26:08 +0200</pubDate>

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFr开发者_如何学PythonomString:currentDate];
NSLog(@"DateObject : '%@'", date);
[dateFormatter release];

I always get

 [44157:10d03] DateObject : '(null)'


You'll need:

  • dd instead of d, since the day is zero-padded
  • ZZ instead of Z, since it doesn't include the timezone abbreviated name

More info: http://unicode.org/reports/tr35/tr35-4.html#Date_Format_Patterns


When I run this:

int main (int argc, const char * argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *str = @"Fri, 09 Sep 2011 15:26:08 +0200";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
    NSDate *date = [dateFormatter dateFromString:str];
    NSLog(@"DateObject : %@", date);
    [dateFormatter release];

    [pool drain];
    return 0;
}

I get this:

EmptyFoundation[19926:407] DateObject : 2011-09-09 13:26:08 +0000


First you could try the following code:

NSString *str = @"Mon, 19 Oct 2015 14:27:28 +0800";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSDate *date = [dateFormatter dateFromString:str];
NSLog(@"DateObject : %@", date);

However, If your DateObject is still null , your locale is not correctly located "en_US_POSIX" format. You need change your locale.

NSString *str = @"Mon, 19 Oct 2015 14:27:28 +0800";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
NSDate *date = [dateFormatter dateFromString:str];
NSLog(@"DateObject : %@", date);

The result is:

DateObject : 2015-10-19 06:27:28 +0000


Use :

  NSArray *array=[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

to split the pubDate by whitespaces and newline and then get every element you want to show by using the indexes of the array.


I use this code to format Wordpress RSS feed dates:

 + (NSDate *)parseWordpressDate:(NSString *)dataWp {

    NSArray *array=[dataWp componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    //Tue, 04 Mar 2014 11:00:36 +0000

    //day = index 1, month = 2, year = 3
    NSString *yyear = [array objectAtIndex:3];
    NSString *mmonth = [array objectAtIndex:2];
    NSString *dday = [array objectAtIndex:1];

    NSDateFormatter *dateFormatterGMTAware = [[NSDateFormatter alloc] init];
    [dateFormatterGMTAware setDateFormat:@"yyyy-MMM-dd"];
        NSString *dateString = [NSString stringWithFormat:@"%@-%@-%@",yyear,mmonth,dday];
        NSDate *data = [dateFormatterGMTAware dateFromString:dateString];
        return data;

  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜