我有一个功能完善的clock.js小部件,用于在多个州的整个办公室的多个显示器上显示日期和时间。

东部时区的办公室没有问题,因为它默认为东部时间(我们为每个显示器运行屏幕的服务器均为东部)。

但是,我想在此处添加一个条件(例如if $screenID == 3 {...,以便在中央时区的屏幕上显示正确的中央时间。

我应该如何在此处添加一个街区以使该状态显示中部而不是东部?

 function startTime() {
    var today = new Date();
    var hr = today.getHours();
    var min = today.getMinutes();
    // var sec = today.getSeconds();
    ap = (hr < 12) ? "<span>AM</span>" : "<span>PM</span>";
    hr = (hr == 0) ? 12 : hr;
    hr = (hr > 12) ? hr - 12 : hr;
    //Add a zero in front of numbers<10
    hr = checkTime(hr);
    min = checkTime(min);
    // sec = checkTime(sec);
    document.getElementById("clock").innerHTML = hr + ":" + min + " " + ap;

    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
    var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    var curWeekDay = days[today.getDay()];
    var curDay = today.getDate();
    var curMonth = months[today.getMonth()];
    // var curYear = today.getFullYear();
    var date = curWeekDay+", "+curDay+" "+curMonth;
    document.getElementById("date").innerHTML = date;

    var time = setTimeout(function(){ startTime() }, 500);
}
function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

最佳答案

使用时区。



function startTime(screen, loc) {
  var timeZone = "America/Chicago";
  if (screen === 1)
    timeZone = "America/New_York";

  var dateOptions = { weekday: 'long', day: 'numeric', month: 'long', timeZone: timeZone };
  var timeOptions = { hour: 'numeric', minute: 'numeric', timeZone: timeZone };
  var dt = new Date();

  document.getElementById("myclock" + loc).innerHTML = dt.toLocaleString("en-US", timeOptions);
  document.getElementById("mydate" + loc).innerHTML = dt.toLocaleString("en-NZ", dateOptions);
}

startTime(0, 1);
startTime(1, 2);

<div id="myclock1">asdf</div>
<div id="mydate1">asdf</div>
<hr>
<div id="myclock2">asdf</div>
<div id="mydate2">asdf</div>

关于javascript - clock.js,有条件地设置东部或东部时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56360630/

10-10 15:59