通过NSURL获取网络标准时间

时间:2023-12-17 15:35:56
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
[connection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"response%@",response);
NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dic=[httpResponse allHeaderFields];
NSLog(@"dic:%@",dic);
NSString *time=[dic objectForKey:@"Date"];
NSString *time1=[time substringFromIndex:16];
NSString *time2=[time1 substringToIndex:9];
NSArray*array=[time2 componentsSeparatedByString:@":"];
int hour=[[array objectAtIndex:0] intValue];
int minute=[[array objectAtIndex:1] intValue];
NSString *now=[NSString stringWithFormat:@"%0.2d:%0.2d",hour+8,minute];
NSLog(@"date:%@",now);
}
}