我在我的应用程序中使用 AFFeedParser 来解析来自不同 HTML 页面的文章。
通过解析,我得到了书面article DATE like
"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t"
作为一个字符串。
我怎样才能把它转换成日期Thursday,December 06,2012
呢?
NSString *dateStr=@"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t";
NSString *getDate=[[dateStr componentsSeparatedByString:@"\\"]objectAtIndex:0];
在这里,您应该注意日期和时间的本地表示,因此您应该使用。
NSDateFormatter dateFormat = [NSDateFormatter dateFormatFromTemplate:@"yMMMMd" options:0 locale:[NSLocale systemLocale]];
NSDate *date=[dateFormat dateFromString:getDate];
NSLog(@"dateString = %@", [dateFormat stringFromDate:date]);
也看日志,我明白了...
NSString *yourStringDate = @"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t";
NSString *str =[yourStringDate stringByReplacingOccurrencesOfString:@"\n" withString:@""];
str =[str stringByReplacingOccurrencesOfString:@"\t" withString:@""];
[str retain];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [dateFormatter dateFromString: str];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEEE,LLLL dd,yyyy"];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);
输出是:转换后的字符串:2012 年 12 月 6 日,星期四
另请参阅我的另一个从字符串获取 NSDate 的答案,但您只需像上面那样设置格式,只需从该格式更改格式... convert-string-to-nsdate-with-custom-format
我希望这对你有帮助....
找到答案:
NSString *inputDate=@"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t";
NSString *trimDate=[[inputDate componentsSeparatedByString:@"+"]objectAtIndex:0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:trimDate];
[dateFormatter setDateFormat:@"EEEE, MMMM dd,yyyy"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString = %@", dateString);
输出:
dateString = Thursday,December 06,2012