2010-05-18 53 views
0

我有一個問題,也許有人可以幫助我,我將解釋...如何設置時間爲特定的地方在JavaScript

  1. 我有在javascript「VAR日期=新的日期( );」它給我的當地時間(瀏覽器時間),但我想迫使這個數據/時間爲特定的本地...例如...西班牙。我希望每次有人進入頁面(來自其他國家)日期需要西班牙時間。

我發現了一些soluction但問題是夏令時和冬令時間......我們已抵消了變化,因爲有些時候是+1小時,別人是+2 ....

有人能夠幫助我在一個解決方案?

感謝 [email protected]

回答

0

看看這篇文章:https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html

從解禁直出:

function calcTime(city, offset) { 

// create Date object for current location 
d = new Date(); 

// convert to msec 
// add local time zone offset 
// get UTC time in msec 
utc = d.getTime() + (d.getTimezoneOffset() * 60000); 

// create new Date object for different city 
// using supplied offset 
nd = new Date(utc + (3600000*offset)); 

// return time as a string 
return "The local time in " + city + " is " + nd.toLocaleString(); 

} 

這樣稱呼它:alert(calcTime('Bombay', '+5.5')); // get Bombay time

你可能需要添加一些邏輯來考慮夏令時。

+0

謝謝,但我已經檢查了這個代碼,但我的問題是在這種情況下,你說的差別是5小時和一半...每次,但有時是少或多(我不知道孟買: ) 我該怎麼做?硬編碼邏輯? 2010年?然後2011 .. BC可能每個不同的一年更改小時的日期是不同的。 謝謝:d – Joao 2010-05-18 07:55:28

+0

我不知道任何可以爲你工作的庫。 Google爲自己編寫邏輯返回了相當數量的結果:http://www.google.co.uk/search?q=javascript+daylight&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en- GB:官方與客戶端= firefox的-A – 2010-05-18 08:13:08

相關問題