[問題] 從網頁抓目前時間

看板MacDev作者 (可樂果)時間11年前 (2013/10/24 19:10), 編輯推噓4(402)
留言6則, 3人參與, 最新討論串1/1
-(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

10/24 19:47, , 1F
抓時間為何不用 [NSDate date] ?有什麼特殊原因嗎
10/24 19:47, 1F
其實有先用這方式 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)

10/24 20:49, , 2F
這就不知道了orz...你要不要乾脆用regex去parse網站最快
10/24 20:49, 2F

10/24 21:25, , 3F
請問k大 regex? 還是ntp怎麼抓時間阿 像這個網址有時間
10/24 21:25, 3F

10/24 21:25, , 4F
10/24 21:25, 4F

10/24 23:52, , 5F
regex就正規表達式囉~純抓時間應該不男
10/24 23:52, 5F

10/25 08:56, , 6F
NTP的話你要先找到台灣有提供的伺服器再連過去抓
10/25 08:56, 6F
文章代碼(AID): #1IQG0hRg (MacDev)
文章代碼(AID): #1IQG0hRg (MacDev)