【问题标题】:Set a GMT on a countdown script在倒计时脚本上设置 GMT
【发布时间】:2018-08-24 08:28:40
【问题描述】:

如何将 GMT 设置为 date.Now()?

var countDownDate = getNextDayOfWeek(new Date(),0,21);

// Update the count down every 1 second
var x = setInterval(function() {
    // Get todays date and time
    var now = new Date().getTime();

    // Find the distance between now an the count down date
    var distance = countDownDate - now;

    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24)).toString();
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)).toString();
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)).toString();
    var seconds = Math.floor((distance % (1000 * 60)) / 1000).toString();

    // Add 0 when value are < 10
    hours = (hours < 10) ? "0"+hours : hours;
    minutes = (minutes < 10) ? "0"+minutes : minutes;
    seconds = (seconds < 10) ? "0"+seconds : seconds;

    var grb = jq("#grb");
    // Display the result in the element with id="grb"
    grb.html(days + "d " + hours + "h " + minutes + "m " + seconds + "s");

    // If the count down is finished, write some text 
    if (distance < 0) {
        clearInterval(x);
        var one_hour = -60 * -60 * -1000;
        if(distance < one_hour){

            grb.html("GRB is finished!");
        }else{
            grb.html("GRB is open!");
        }
    }
}, 1000);

});

它一直在使用你电脑的时间,我想每次都使用一个时区来存储。

我已经尝试过 TimeZoneOffset,但没有成功,我们将不胜感激。

【问题讨论】:

  • getNextDayOfWeek 返回什么?使用间隔为 1000 的 setInterval 并不能保证它会以精确的一秒间隔运行。观察夏令时的日子并非都是 24 小时。 getTimezoneOffset 方法根据主机系统设置返回时区偏移量,它与 Date 对象无关。日期没有时区偏移属性,不能设置。
  • @Blutengel - 您的代码中没有任何内容适用于当地时间。请展示getNextDayOfWeek的实现。您的问题可能会在那里得到解决。

标签: javascript timer timezone countdown


【解决方案1】:

请看这里

var now = new Date();
var nowUTC = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var distance = end - nowUTC;

【讨论】:

  • 纯代码答案没有帮助。您应该解释为什么 OP 有问题以及您的代码如何修复它。
  • 您好,它不工作,因为它没有显示 GMT+8。
猜你喜欢
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
相关资源
最近更新 更多