我在我的应用中使用了AFFeedParser来解析来自不同HTML页面的文章。
通过解析,我得到了书面article DATE like
"Thu, 06 Dec 2012 17:44:22 +0000\n\t\t"
作为字符串。
如何将此转换为日期,如Thursday,December 06,2012
?
答案 0 :(得分:2)
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]);
答案 1 :(得分:1)
另见Log,我明白了......
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);
OUTPUT IS:转换字符串:2012年12月6日星期四
另请参阅我从字符串中获取NSDate的另一个答案,但是您只需设置上面的格式,只需更改格式... convert-string-to-nsdate-with-custom-format
我希望这对你有帮助....
答案 2 :(得分:0)
找到答案:
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