Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我想为特定的时区创建一个javascript时钟_Javascript_Time_Clock - Fatal编程技术网

I';我想为特定的时区创建一个javascript时钟

I';我想为特定的时区创建一个javascript时钟,javascript,time,clock,Javascript,Time,Clock,我想在我的网站上添加一个时钟,在中央时区显示时间。到目前为止,我已经包括了代码。我想用它来告诉用户我的网站上的其他工具何时将处于活动状态(它们在中央时区的夜间不处于活动状态)。是否有人知道如何(1)将此锁定到中央时间;(2)从晚上8:00到早上7:30,把它变成红色(表示工具已经关闭) 函数GetClock(){ d=新日期(); nhour=d.getHours(); nmin=d.getMinutes(); 如果(nhour==0){ap=“AM”;nhour=12;} 如果(nhour=

我想在我的网站上添加一个时钟,在中央时区显示时间。到目前为止,我已经包括了代码。我想用它来告诉用户我的网站上的其他工具何时将处于活动状态(它们在中央时区的夜间不处于活动状态)。是否有人知道如何(1)将此锁定到中央时间;(2)从晚上8:00到早上7:30,把它变成红色(表示工具已经关闭)


函数GetClock(){
d=新日期();
nhour=d.getHours();
nmin=d.getMinutes();
如果(nhour==0){ap=“AM”;nhour=12;}
如果(nhour=13){ap=“PM”;nhour-=12;}

如果(nmin如果创建本地日期对象,它将位于本地系统的时区中(无论设置为什么,它可能不是实际的本地时区)。日期对象有一个getTimezoneOffset方法,该方法返回一个以分钟为单位的数,如果添加到本地日期对象中,将其设置为UTC(基本上是GMT)。然后,您可以减去“中心时间”(不管是什么)的偏移量,得到该时区的时间

然后可以将该日期对象用于该区域中的时间

如果时区为美国中央时区,则标准偏移量为-6小时,夏令时偏移量为-5小时。返回具有特定偏移量的日期对象的函数为:

/* Create a date object with the desired offset.
   Offset is the time that must be added to local time to get
   UTC, so if time zone is -6hrs, offset is +360. If time zone
   is +10hrs, offset is -600.
*/
function getOffsetDate(offsetInMintues) {
  // Get local date object
  var d = new Date();
  // Add local time zone offset to get UTC and 
  // Subtract offset to get desired zone
  d.setMinutes(d.getMinutes() + d.getTimezoneOffset() - offsetInMintues);
  return d;
}
为其指定适当的偏移量,它将返回具有该偏移量的日期对象。要获取美国标准中心时间,请执行以下操作:

var centralDate = getOffsetDate(360);
对于美国中央夏令时:

var centralDSTDate = getOffsetDate(300);
要在特定时间之间执行某些操作,您可以执行以下操作:

var h = centralDate.getHours();
var m = centralDate.getMinutes(); 
if (h >= 20 || 
  h <7 ||
  (h == 7 && m <= 30) {
  // the time is between 20:00 and 07:30 the following day.
}
var h=centralDate.getHours();
var m=centralDate.getMinutes();
如果(h>=20 | |

h日期对象可以修改为您喜欢的任何值,并将自动更正。 “windTheClock(2);”将时区偏移设置为+2 UTC

<script type="text/javascript">

function addLeadingZero(n) {
    if (n < 10) {
        n = "0" + n;
    }
    return n;
}

function windTheClock(timeZoneOffset)
{
    var d = new Date();
    d.setHours(d.getUTCHours() + timeZoneOffset); // set time zone offset
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    h = addLeadingZero(h);
    m = addLeadingZero(m);
    s = addLeadingZero(s);
    document.all["clock"].innerHTML = h + ":" + m + ":" + s;
    setTimeout(function(){ windTheClock(timeZoneOffset) }, 1000);
}

window.onload = function() {
    windTheClock(2);
}

</script>

<div id="clock"></div>

函数addLeadingZero(n){
如果(n<10){
n=“0”+n;
}
返回n;
}
功能windTheClock(时区偏移)
{
var d=新日期();
d、 设置小时数(d.getUTCHours()+时区偏移量);//设置时区偏移量
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
h=添加引线零(h);
m=添加引线零(m);
s=添加引线零(s);
document.all[“clock”].innerHTML=h+“:“+m+”:“+s;
setTimeout(函数(){windTheClock(timeZoneOffset)},1000);
}
window.onload=函数(){
风挡(2);
}

版本w/am/pm

function addLeadingZero(n) {
    return n < 10 ? '0' + n : n;
}

function windTheClock(timeZoneOffset)
{
    var d = new Date();
    d.setHours(d.getUTCHours() + timeZoneOffset); // set time zone offset
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    var ampm = h >= 12 ? 'pm' : 'am';
    h = h % 12;
    h = h ? h : 12; // replace '0' w/ '12'
    h = addLeadingZero(h);
    m = addLeadingZero(m);
    s = addLeadingZero(s);

    document.all["clock"].innerHTML = h + ':' + m + ':' + s
        + ' ' + ampm;
    setTimeout(function(){ windTheClock(timeZoneOffset) }, 1000);
}

window.onload = function() {
    windTheClock(2);
}
函数addLeadingZero(n){
返回n<10?'0'+n:n;
}
功能windTheClock(时区偏移)
{
var d=新日期();
d、 设置小时数(d.getUTCHours()+时区偏移量);//设置时区偏移量
var h=d.getHours();
var m=d.getMinutes();
var s=d.getSeconds();
var ampm=h>=12?'pm':'am';
h=h%12;
h=h?h:12;//替换“0”w/“12”
h=添加引线零(h);
m=添加引线零(m);
s=添加引线零(s);
document.all[“clock”].innerHTML=h+':'+m+':'+s
+''+ampm;
setTimeout(函数(){windTheClock(timeZoneOffset)},1000);
}
window.onload=函数(){
风挡(2);
}

谢谢。效果很好,但如何显示上午/下午12小时格式?
function addLeadingZero(n) {
    return n < 10 ? '0' + n : n;
}

function windTheClock(timeZoneOffset)
{
    var d = new Date();
    d.setHours(d.getUTCHours() + timeZoneOffset); // set time zone offset
    var h = d.getHours();
    var m = d.getMinutes();
    var s = d.getSeconds();
    var ampm = h >= 12 ? 'pm' : 'am';
    h = h % 12;
    h = h ? h : 12; // replace '0' w/ '12'
    h = addLeadingZero(h);
    m = addLeadingZero(m);
    s = addLeadingZero(s);

    document.all["clock"].innerHTML = h + ':' + m + ':' + s
        + ' ' + ampm;
    setTimeout(function(){ windTheClock(timeZoneOffset) }, 1000);
}

window.onload = function() {
    windTheClock(2);
}