2013-01-08 21 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> 

将当前浏览器时区偏移量添加到浏览器时间,然后减去所需的时区偏移量以获取所需的时间。

您需要的时区需要以某种方式传递给浏览器才能正常工作。

相关问题