iPhone开发笔记(18)将Http Header中的Date转换成NSDate

    1、获取Http Header

我这里是在AppDelegate中定义了一个ASIDownloadCache的全局变量myCache

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSDictionary *dic = [appDelegate.myCache cachedResponseHeadersForURL:[NSURL URLWithString:@"http://....../getPictureNews.aspx"]];
输出dic,得到如下的结果:

{
    "Cache-Control" = private;
    "Content-Length" = 2404;
    "Content-Type" = "text/html;charset=utf-8";
    Date = "Sun, 07 Oct 2012 06:13:41 GMT";
    Server = "Microsoft-IIS/6.0";
    "X-ASIHTTPRequest-Fetch-date" = "Sun, 07 Oct 2012 06:13:44 GMT";
    "X-AspNet-Version" = "2.0.50727";
    "X-Powered-By" = "ASP.NET";
}
    2、获取Date并转化

NSString *lastUpdate = [dic objectForKey:@"Date"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"];
NSDate *date = [formatter dateFromString:lastUpdate];
NSLog(@"%@",date);
输出date的结果为

2012-10-07 06:13:44 +0000

    3、总结

    关键是在NSDateFormatter的formatter String要写正确,最后得到date之后就可以做其他的事情了,进行其他日期操作就没什么区别了。

你可能感兴趣的:(iPhone开发笔记,date,header,iphone,asp.net,sun,string)