世界时钟没有显示正确的时间

时间:2018-03-15 17:16:07

标签: javascript jquery

我正在尝试为三个不同的时区显示数字时钟,洛杉矶,丹佛和纽约。我遇到的问题是,如果用户位于洛杉矶,丹佛时钟显示洛杉矶时间而不是正确的丹佛时间,这也让我相信纽约时间也可能会关闭。有没有办法调整我的javascript,以便时间不是基于用户位置,而是基于我试图显示的实际时区,无论用户位于何处?

$(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('denver');
clockDiv.innerText = hours + ":" + minutes + meridiem;
}

timeDisplay();
setInterval(timeDisplay, 1000);



//function to display time in (EST) New York 
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); 

});

HTML

<li>
    <span class="updated">
         <span class="stat stat-nyc">
             <span class="stat-content stat-content-nyc"><span id="newYork"></span></span>
         </span>&nbsp;
    </span>
</li>

<li>
    <span class="updated">
         <span class="stat stat-den">
             <span class="stat-content stat-content-den"><span id="denver"></span></span>
         </span>&nbsp;
    </span>
</li>

<li>
    <span class="updated">
         <span class="stat stat-la">
             <span class="stat-content stat-content-la"><span id="losAngeles"></span></span>
         </span>&nbsp;
    </span>
</li>

0 个答案:

没有答案