0

我的网站上有一个实时时钟,它使用 JavaScript 运行,但是它从用户的计算机上获取当前时间,因此每个用户的时钟工作时区不同。如何修改下面的代码以使时钟仅使用美国中部标准时间 (GMT - 6)?

function updateClock () {
    var currentTime = new Date();
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();
    var currentSeconds = currentTime.getSeconds();

    currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
    currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

    var timeOfDay = (currentHours < 12) ? "AM" : "PM";
    currentHours = (currentHours > 12) ? currentHours-12 : currentHours;
    currentHours = (currentHours == 0) ? 12 : currentHours;

    var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

    document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}
4

3 回答 3

1

此示例每秒检查分钟,并在分钟更改时更新显示时间。在 2006 年之前是不准确的,这是美国最后一次调整 DST。

<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>US Time Zones</title>
<style>
p{max-width:700px;font-size:1.25em}
</style>
<script>

Date.short_months= ['Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
Date.tzones={
    N:['Newfoundland', -210],
    A:['Atlantic', -240],
    E:['Eastern', -300],
    C:['Central', -360],
    M:['Mountain', -420],
    P:['Pacific', -480],
    AK:['Alaska', -540],
    HA_:['Hawaii-Aleutian (Aleutian)', -600],
    HA:['Hawaii-Aleutian (Hawaii)', -600, -600]
};
Date.dstOff= function(d, tz){
    var off= tz[1], doff= tz[2],
    countstart, countend, dstart, dend,
    y= d.getUTCFullYear();
    if(y>2006 && off!== doff){
        countstart= 8, countend= 1,
        dstart= new Date(Date.UTC(y, 2, 8, 2)),
        dend= new Date(Date.UTC(y, 10, 1, 2));
        while(dstart.getUTCDay()!== 0){
            dstart.setUTCDate(++countstart);
        }
        while(dend.getUTCDay()!== 0){
            dend.setUTCDate(++countend);
        }
        dstart.setUTCMinutes(off);
        dend.setUTCMinutes(off);
        if(dstart<= d && dend>= d) off= doff;
    }
    return off;
}
Date.toTZString= function(d, tzp){
    d= d? new Date(d):new Date();
    tzp= tzp || 'G';
    var h, m, s, pm= 'pm', off, dst, str,
    label= tzp+'ST',
    tz= Date.tzones[tzp.toUpperCase()];
    if(!tz) tz= ['Greenwich', 0, 0];
    off= tz[1];
    if(off){
        if(tz[2]== undefined) tz[2]= tz[1]+60;
        dst= Date.dstOff(d, tz);
        if(dst!== off) label= tzp+'DT';
        d.setUTCMinutes(d.getUTCMinutes()+dst);
    }
    else label= 'GMT';
    h= d.getUTCHours();
    m= d.getUTCMinutes();
    if(h>12) h-= 12;
    else if(h!== 12) pm= 'am';
    if(h== 0) h= 12;
    if(m<10) m= '0'+m;
    var str= Date.short_months[d.getUTCMonth()]+' '+d.getUTCDate()+', ';
    return str+ h+':'+m+' '+pm+' '+label.replace('_', '').toUpperCase();
}
window.onload=function(){
    var who=document.getElementById('CentralTimer');
    who.firstChild.data= Date.toTZString('', 'C');
    Date.ctclock= setInterval(function(){
        var v=who.firstChild.data,
        t=Date.toTZString('', 'C');
        if(v!=t) who.firstChild.data=t;
    },1000);
who.ondblclick=function(){
    clearInterval(Date.ctclock);
    who.firstChild.data+=' (Clock Stopped)';
}
}
</script>
</head>
<body>
<h1 id="CentralTimer">Central Time</h1>


</body>
</html>
于 2012-10-02T06:02:33.913 回答
0

进行自定义时区时钟的一种方法是根本不使用本地时间,而是在将日期移动您选择的时差后始终从 UTC 计算。

这是您的代码修改为使用.getUTC...和向前推进 DST(使用 2007 年的美国规则)

function updateClock () {
    var d = null,                  // date
        h = 0, m = 0, s = 0,       // integers
        sh = '', sm = '', ss = '', // strings
        tod = 'AM',
        timeString = '',
        month = 0, dom = 0,        // used for DST calculation
        dos = 0, DST = false,
        offset = -21600000;        // -6 hours in ms

    d = new Date( (new Date()).valueOf() + offset ); // create date moved by offset

    h = d.getUTCHours(), m = d.getUTCMinutes(), s = d.getUTCSeconds();

    // --- calculate DST ---
    // needs to be modified for different regions ( currently US )
    // remove or comment out if you don't need DST

    month = d.getUTCMonth(), dom = d.getUTCDate(), dos = dom - d.getUTCDay();
    // dos is dom of most recent Sunday ( dos <= 0 means last month )
    if ( month === 2 ) {                    // March
        if ( dom > 14 ) DST = true;         // after second week, definately DST
        else if ( dos > 7 ) {               // Sunday was in second week
            if ( dom === dos ) {            // case for day of change
                if( h > 1 ) DST = true;
            } else DST = true;
        }
    } else if ( month === 10 ) {            // November
        if ( dom < 8 ) {                    // first week
            if ( dos < 1 ) DST = true;      // no Sunday yet, definately DST
            else if ( dos === dom &&        // case for day of change
                h < 1 ) DST = true;
        }
    } else if ( month > 2 && month < 10 )   // between March & November
        DST = true;                         // definately DST

    if ( DST ) {               // in DST
        h = h + 1;             // advance clock one hour
        if ( h === 24 ) h = 0; // allow for AM/PM check
    }

    // --- end of DST calculation ---

    ss = (s < 10 ? '0' : '') + s; // pad
    sm = (m < 10 ? '0' : '') + m; // pad

    if ( h >= 12 ) {   // convert to 12 hour
        tod = 'PM';
        h = h - 12;
    }
    if( h === 0 ) h = 12;
    sh = (h < 10 ? '0' : '') + h; // pad

    timeString = sh + ':' + sm + ':' + ss + ' ' + tod; // build final string

    document.getElementById("clock").firstChild.nodeValue = timeString;
    return timeString;
}
于 2012-10-02T06:12:31.543 回答
0

每个人都提到的那条线是

var offset = new Date().getTimezoneOffset();

这将设置offset为一个数字,等于客户端时区和 GMT 之间的差异(以分钟为单位)。对于 CST,这应该是 300 或 360,具体取决于夏令时。客户和 CST 偏移之间的差异offset是您需要从日期中添加/减去的分钟数。

日期数学相对简单:

var minutesToAdd = 300;  //can also be negative
var now = new Date();
now.setMinutes(now.getMinutes() + minutesToAdd);
//now is five hours ahead

结合起来,这两部分应该可以满足您的需求。

于 2012-10-02T03:49:34.870 回答