2013-01-08 20 views
0

我想在我的網站上有一個計時器,就像數字時鐘一樣。它會有一個日期和時間。我有以下代碼來做到這一點:Javascript在我的網站上使用UTC顯示日期和時間來考慮用戶在javascript中的時區

var clockID = 0; 

function UpdateClock() { 
    if(clockID) { 
     clearTimeout(clockID); 
     clockID = 0; 
    } 

    var tDate = new Date(); 
    var in_hours = tDate.getHours() 
    var in_minutes=tDate.getMinutes(); 
    var in_seconds= tDate.getSeconds(); 

    if(in_minutes < 10) 
     in_minutes = '0'+in_minutes; 
    if(in_seconds<10) 
     in_seconds = '0'+in_seconds; 
    if(in_hours<10) 
     in_hours = '0'+in_hours; 

    document.getElementById('theTime').innerHTML = "" 
        + in_hours + ":" 
        + in_minutes + ":" 
        + in_seconds; 

    clockID = setTimeout("UpdateClock()", 1000); 
} 
function StartClock() { 
    clockID = setTimeout("UpdateClock()", 500); 
} 

function KillClock() { 
    if(clockID) { 
     clearTimeout(clockID); 
     clockID = 0; 
    } 
} 

但是,這段代碼顯示當前的計算機時間,所以時間不適合我的時區。我還需要在代碼中添加哪些內容才能根據我的時區顯示日期和時間?如果日期是DST,則會在DST中顯示確切的時間。

+0

你是什麼意思服務器時間?它會顯示瀏覽器運行的計算機的時間 – mplungjan

+0

是電腦時間,很抱歉。我要編輯它。 – user1149244

+0

所以你需要用你的服務器時間播種它 – mplungjan

回答

1

如果您只需要使用JS,看看

add or subtract timezone difference to javascript Date

例如DEMO

var clockID; 
var yourTimeZoneFrom = -7.00; //time zone value where you are at 

var d = new Date(); 
//get the timezone offset from local time in minutes 
var tzDifference = yourTimeZoneFrom * 60 + d.getTimezoneOffset(); 
//convert the offset to milliseconds 
var offset = tzDifference * 60 * 1000; 

function UpdateClock() { 
    var tDate = new Date(new Date().getTime()+offset); 
    var in_hours = tDate.getHours() 
    var in_minutes=tDate.getMinutes(); 
    var in_seconds= tDate.getSeconds(); 

    if(in_minutes < 10) 
     in_minutes = '0'+in_minutes; 
    if(in_seconds<10) 
     in_seconds = '0'+in_seconds; 
    if(in_hours<10) 
     in_hours = '0'+in_hours; 

    document.getElementById('theTime').innerHTML = "" 
        + in_hours + ":" 
        + in_minutes + ":" 
        + in_seconds; 

} 
function StartClock() { 
    clockID = setInterval(UpdateClock, 500); 
} 

function KillClock() { 
    clearTimeout(clockID); 
} 
window.onload=function() { 
    StartClock(); 
} 
+0

謝謝你,但它也可能在JavaScript? – user1149244

+0

查看最新更新 - 測試 – mplungjan

1

在JS,你可以得到當前時區偏移。你需要調整偏移到所需的時間區域。

<div id="theTime"></div> 
<script> 
    var clockID = 0; 
    var requiredTimeZone = 360; // CST (+6:00 hrs) 

    function UpdateClock() { 
    if(clockID) { 
     clearTimeout(clockID); 
     clockID = 0; 
    } 

     var tDate = new Date(); 
     var calculatedTime = tDate.getTime() + (tDate.getTimezoneOffset() * 60000) - (requiredTimeZone * 60000); 
     tDate.setTime(calculatedTime); 
     var in_hours = tDate.getHours() 
     var in_minutes=tDate.getMinutes(); 
     var in_seconds= tDate.getSeconds(); 

     if(in_minutes < 10) 
      in_minutes = '0'+in_minutes; 
     if(in_seconds<10) 
      in_seconds = '0'+in_seconds; 
     if(in_hours<10) 
      in_hours = '0'+in_hours; 

    document.getElementById('theTime').innerHTML = "" 
        + in_hours + ":" 
        + in_minutes + ":" 
        + in_seconds; 

    clockID = setTimeout("UpdateClock()", 1000); 
    } 
    function StartClock() { 
    clockID = setTimeout("UpdateClock()", 500); 
    } 

    function KillClock() { 
    if(clockID) { 
     clearTimeout(clockID); 
     clockID = 0; 
    } 
    } 
    StartClock(); 
</script> 

將當前瀏覽器時區偏移量添加到瀏覽器時間,然後減去所需的時區偏移量以獲取所需的時間。

您需要的時區需要以某種方式傳遞給瀏覽器才能正常工作。

相關問題