作者starkite (可樂果)
看板MacDev
標題[問題] 從網頁抓目前時間
時間Thu Oct 24 19:10:32 2013
-(void)getcurrentdate{
NSURL *url=[NSURL URLWithString:@"
http://www.google.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
{
NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dic=[httpResponse allHeaderFields];
NSLog(@"dic:%@",dic);
NSString *time=[dic objectForKey:@"Date"];
}
小的使用這方式可以正確抓到時間目前,抓到的月份是英文的,
請問應該怎麼顯示數字的月份?用NSDateFormatter更改格式也沒用
上述的方式可以用afnetworking來取得嗎?
AFHTTPRequestOperation只是回傳整個網頁的html回來 沒有時間
上述的方式實在太慢了......
麻煩大大們幫忙! 謝謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.162.165
1F:推 kiii210:抓時間為何不用 [NSDate date] ?有什麼特殊原因嗎 10/24 19:47
其實有先用這方式
NSDate *now = [NSDate date];
NSDateFormatter *TaipeiDateFormatter = [[NSDateFormatter alloc] init];
[TaipeiDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[TaipeiDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Taipei"]];
//[TaipeiDateFormatter setDateStyle:NSDateFormatterMediumStyle];
//[TaipeiDateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *TaipeiDateStr = [TaipeiDateFormatter stringFromDate:now];
//NSLog(@"TaipeiDateStr is %@",TaipeiDateStr);
NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [dateFormatter dateFromString:TaipeiDateStr];
NSLog(@"dateFromString1 is %@",dateFromString1);
這樣子不管使用者的時區在哪都可以用台灣的時間計算
但是另外的問題是使用者自己往前或往後調了10分鐘
這樣子就不是正確的時間
還有有任何offset的方式嗎?
※ 編輯: starkite 來自: 1.34.125.154 (10/24 20:20)
2F:推 kiii210:這就不知道了orz...你要不要乾脆用regex去parse網站最快 10/24 20:49
3F:→ starkite:請問k大 regex? 還是ntp怎麼抓時間阿 像這個網址有時間 10/24 21:25
5F:推 kiii210:regex就正規表達式囉~純抓時間應該不男 10/24 23:52
6F:推 charlesdc:NTP的話你要先找到台灣有提供的伺服器再連過去抓 10/25 08:56