【问题标题】:Javascript countdown that is the same for people in every region每个地区的人都一样的Javascript倒计时
【发布时间】:2020-11-08 18:54:59
【问题描述】:

目前我有一个倒数计时器,唯一的问题是我输入的日期和时间不会与居住在其他地方的人同时结束,因此会将 javascript 解释为他们的时区。有没有办法解决这个问题?当前脚本:

var countDownDate = new Date("Jul 30, 2020 20:00:00").getTime();


var x = setInterval(function() {


  var now = new Date().getTime();


  var distance = countDownDate - now;


  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);


  document.getElementById("countdown").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";


  if (distance < 0) {
    clearInterval(x);
    document.getElementById("countdown").innerHTML = "EXPIRED";
  }
}, 1000);
&lt;p id="countdown"&gt;&lt;/p&gt;

【问题讨论】:

    标签: javascript html countdown clock countdowntimer


    【解决方案1】:

    为您的字符串添加一个明确的时区(例如 UTC-0000)

    // Add an explicit timezone to your string (eg. UTC-0000)
    var countDownDate = new Date("Jul 30, 2020 20:00:00 UTC-0000").getTime();
    
    
    var x = setInterval(function() {
    
    
      var now = new Date().getTime();
    
    
      var distance = countDownDate - now;
    
    
      var days = Math.floor(distance / (1000 * 60 * 60 * 24));
      var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = Math.floor((distance % (1000 * 60)) / 1000);
    
    
      document.getElementById("countdown").innerHTML = days + "d " + hours + "h "
      + minutes + "m " + seconds + "s ";
    
    
      if (distance < 0) {
        clearInterval(x);
        document.getElementById("countdown").innerHTML = "EXPIRED";
      }
    }, 1000);
    &lt;p id="countdown"&gt;&lt;/p&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-24
      • 2011-08-01
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      相关资源
      最近更新 更多