2012-10-02 20 views
0

我在我的网站上有一个实时时钟,它使用JavaScript来运行,但它从用户的计算机抓取当前时间,因此时钟工作的时区对于每个用户都不相同。我如何修改下面的代码以便时钟仅使用美国中部标准时间(GMT - 6)?如何让javascript时钟只显示一个时区?

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; 
} 
+2

http://stackoverflow.com/q/1091372/362536 – Brad

+0

@Brad也是帮助一点点,但我是新来的JavaScript,所以...不是太多 – user1710563

+0

我不知道如果你不能解释如何先前的问题不直接回答你的问题如何帮助你。解释一下你感到困惑。 – Brad

回答

0

线大家一直提到是

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

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

日期数学相对简单:

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

组合,这两件应该做你所需要的。

+0

注意:您可能想要测试一系列浏览器上的'setMinutes()'位。与0-59范围以外的数字可能有些不兼容。 – smitelli

0

本示例每秒钟检查分钟数,并更新分钟更改时的显示时间。在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> 
0

一种方式做定制timezone'd时钟是不是在所有使用本地时间,但是从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; 
} 
相关问题