我想为特定时区创建一个 javascript 时钟

发布于 2024-12-12 21:56:34 字数 692 浏览 1 评论 0原文

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

<script type="text/javascript">

function GetClock(){
d = new Date();
nhour  = d.getHours();
nmin   = d.getMinutes();
     if(nhour ==  0) {ap = " AM";nhour = 12;} 
else if(nhour <= 11) {ap = " AM";} 
else if(nhour == 12) {ap = " PM";} 
else if(nhour >= 13) {ap = " PM";nhour -= 12;}

if(nmin <= 9) {nmin = "0" +nmin;}

document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+ap+"";
setTimeout("GetClock()", 1000);
}
window.onload=GetClock;
</script>
<div id="clockbox"></div>

I'd like to add a clock to my web site, displaying the time in the Central time zone. I have included the code I have thus far. I want to use this to tell users when other tools on my web site will be active (they are not active during the night in the Central time zone). Does anyone know how to (1) lock this to central time; and (2) perhaps turn it a red color from 8:00 p.m. - 7:30 a.m. (indicating the tools are turned off)?

<script type="text/javascript">

function GetClock(){
d = new Date();
nhour  = d.getHours();
nmin   = d.getMinutes();
     if(nhour ==  0) {ap = " AM";nhour = 12;} 
else if(nhour <= 11) {ap = " AM";} 
else if(nhour == 12) {ap = " PM";} 
else if(nhour >= 13) {ap = " PM";nhour -= 12;}

if(nmin <= 9) {nmin = "0" +nmin;}

document.getElementById('clockbox').innerHTML=""+nhour+":"+nmin+ap+"";
setTimeout("GetClock()", 1000);
}
window.onload=GetClock;
</script>
<div id="clockbox"></div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吝吻 2024-12-19 21:56:37

Date 对象可以修改为您喜欢的任何值,并且会自动更正。
“风时钟(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>

带 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);
}

https://jsfiddle.net/dzyubak/rae8j6xn/

The Date object can be modified to any value you like and will be corrected automatically.
"windTheClock(2);" sets time zone offset to +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>

version 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);
}

https://jsfiddle.net/dzyubak/rae8j6xn/

心碎无痕… 2024-12-19 21:56:36

如果您创建本地日期对象,它将位于本地系统的时区中(无论设置为何,可能不是实际的本地时区)。日期对象有一个 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.
}

If you create a local date object, it will be in the timezone of the local system (whatever that might be set to, which might not be the actual local time zone). Date objects have a getTimezoneOffset method that returns a number in minutes that, if added to the local date object, sets it to UTC (essentially GMT). You can then subtract the offset for "central time" (whatever that might be) to get a time in that timezone.

You can then use that date object for times in that zone.

If the timezone is the US Central time zone, the standard offset is -6 hours, the daylight saving offset is -5 hours. A function that returns a date object with a specific offset is:

/* 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;
}

Give it the appropriate offset and it will return a date object with that offset. To get US standard central time:

var centralDate = getOffsetDate(360);

For US central daylight saving time:

var centralDSTDate = getOffsetDate(300);

To do something between specific times, you can do something like:

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文