使用javascript的当前时间没有抓住正确的位置

时间:2018-02-21 21:07:34

标签: javascript jquery

运行脚本以获取两个特定位置的当前时间。一次是山地时间,另一次是东海岸时间。我遇到了一个问题,如果用户位于PST位置,则山地时间时钟根据太平洋标准时间显示时间。而不是让PST有一种方法让我确保两个时钟只获得MT和ET并考虑夏令时?

$(document).ready(function(){

  function timeDisplay() {
  var currentTime = new Date();
  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();
  //var seconds = currentTime.getSeconds();
  var meridiem = " ";

  if(hours >= 12){
    hours = hours - 12;
    meridiem = "pm";
  }
  else{
    meridiem = "am";
  }
  if(hours === 0){
    hours = 12;
  }
  if(hours < 10){
    hours = "0" + hours;
  }
  if(minutes < 10){
    minutes = "0" + minutes;
  }

  var clockDiv = document.getElementById('stat');
  clockDiv.innerText = hours + ":" + minutes + meridiem;
  }

  timeDisplay();
  setInterval(timeDisplay, 1000);



  function newYorkTimeDisplay(offset) {
  var currentTime = new Date();  
  currentTime.setHours(currentTime.getHours()+offset); 

  var hours = currentTime.getHours();
  var minutes = currentTime.getMinutes();
  //var seconds = currentTime.getSeconds();
  var meridiem = " ";

  if(hours >= 12){
    hours = hours - 12;
    meridiem = "pm";
  }
  else{
    meridiem = "am";
  }
  if(hours === 0){
    hours = 12;
  }
  if(hours < 10){
    hours = "0" + hours;
  }
  if(minutes < 10){
    minutes = "0" + minutes;
  }

  var newYorkDiv = document.getElementById('newYork');      
  newYorkDiv.innerText = hours + ":" + minutes + meridiem; 
}

newYorkTimeDisplay(+2);
setInterval(newYorkTimeDisplay, 1000, +2); 

});

对此有任何帮助表示赞赏。试图找出我所缺少的东西。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您需要获取用户当前的UTC时区偏移量并将其从纽约时区偏移量中减去。

var tz_offset = (new Date().getTimezoneOffset()/100) - 3;
newYorkTimeDisplay(tz_offset);

&#13;
&#13;
$(document).ready(function() {

  function timeDisplay() {
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    //var seconds = currentTime.getSeconds();
    var meridiem = " ";

    if (hours >= 12) {
      hours = hours - 12;
      meridiem = "pm";
    } else {
      meridiem = "am";
    }
    if (hours === 0) {
      hours = 12;
    }
    if (hours < 10) {
      hours = "0" + hours;
    }
    if (minutes < 10) {
      minutes = "0" + minutes;
    }

    var clockDiv = document.getElementById('stat');
    clockDiv.innerText = hours + ":" + minutes + meridiem;
  }

  timeDisplay();
  setInterval(timeDisplay, 1000);



  function newYorkTimeDisplay(offset) {
    var currentTime = new Date();
    currentTime.setHours(currentTime.getHours() + offset);

    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    //var seconds = currentTime.getSeconds();
    var meridiem = " ";

    if (hours >= 12) {
      hours = hours - 12;
      meridiem = "pm";
    } else {
      meridiem = "am";
    }
    if (hours === 0) {
      hours = 12;
    }
    if (hours < 10) {
      hours = "0" + hours;
    }
    if (minutes < 10) {
      minutes = "0" + minutes;
    }

    var newYorkDiv = document.getElementById('newYork');
    newYorkDiv.innerText = hours + ":" + minutes + meridiem;
  }

  var tz_offset = (new Date().getTimezoneOffset()/100) - 3;
  newYorkTimeDisplay(tz_offset);
  
  setInterval(newYorkTimeDisplay, 1000, +2);

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id=newYork></div>
<div id=stat></div>
&#13;
&#13;
&#13;