0

ウェブサイトにデジタル時計のようなタイマーを入れたいです。日付と時刻が含まれます。私はそれを行うために次のコードを持っています:

var clockID = 0;

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }

    var tDate = new Date();
    var in_hours = tDate.getHours()
    var in_minutes=tDate.getMinutes();
    var in_seconds= tDate.getSeconds();

    if(in_minutes < 10)
        in_minutes = '0'+in_minutes;
    if(in_seconds<10)   
        in_seconds = '0'+in_seconds;
    if(in_hours<10) 
        in_hours = '0'+in_hours;

   document.getElementById('theTime').innerHTML = "" 
                   + in_hours + ":" 
                   + in_minutes + ":" 
                   + in_seconds;

   clockID = setTimeout("UpdateClock()", 1000);
}
function StartClock() {
   clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}

しかし、このコードは現在のコンピューターの時刻を表示しているため、時刻が私のタイムゾーンに適合していません。タイムゾーンに従って日付と時刻が表示されるように、コードに他に何を追加する必要がありますか?日付がDSTの場合は、DSTの正確な時刻も表示されます。

4

2 に答える 2

1

JSのみを使用する必要がある場合は、以下をご覧ください。

javascriptの日付にタイムゾーンの差を加算または減算します

例:デモ

var clockID;
var yourTimeZoneFrom = -7.00; //time zone value where you are at

var d = new Date();  
//get the timezone offset from local time in minutes
var tzDifference = yourTimeZoneFrom * 60 + d.getTimezoneOffset();
//convert the offset to milliseconds
var offset = tzDifference * 60 * 1000;

function UpdateClock() {
    var tDate = new Date(new Date().getTime()+offset);
    var in_hours = tDate.getHours()
    var in_minutes=tDate.getMinutes();
    var in_seconds= tDate.getSeconds();

    if(in_minutes < 10)
        in_minutes = '0'+in_minutes;
    if(in_seconds<10)   
        in_seconds = '0'+in_seconds;
    if(in_hours<10) 
        in_hours = '0'+in_hours;

   document.getElementById('theTime').innerHTML = "" 
                   + in_hours + ":" 
                   + in_minutes + ":" 
                   + in_seconds;

}
function StartClock() {
   clockID = setInterval(UpdateClock, 500);
}

function KillClock() {
  clearTimeout(clockID);
}
window.onload=function() {
  StartClock();
}
于 2013-01-08T07:51:50.550 に答える
1

JSでは、現在のタイムゾーンオフセットを取得できます。オフセットを希望のtimeZoneに調整する必要があります。

<div id="theTime"></div>
<script>
  var clockID = 0;
  var requiredTimeZone = 360; // CST (+6:00 hrs)

  function UpdateClock() {
     if(clockID) {
        clearTimeout(clockID);
        clockID  = 0;
     }

      var tDate = new Date();
      var calculatedTime = tDate.getTime() + (tDate.getTimezoneOffset() * 60000) - (requiredTimeZone * 60000);
      tDate.setTime(calculatedTime);
      var in_hours = tDate.getHours()
      var in_minutes=tDate.getMinutes();
      var in_seconds= tDate.getSeconds();

      if(in_minutes < 10)
          in_minutes = '0'+in_minutes;
      if(in_seconds<10)   
          in_seconds = '0'+in_seconds;
      if(in_hours<10) 
          in_hours = '0'+in_hours;

     document.getElementById('theTime').innerHTML = "" 
                     + in_hours + ":" 
                     + in_minutes + ":" 
                     + in_seconds;

     clockID = setTimeout("UpdateClock()", 1000);
  }
  function StartClock() {
     clockID = setTimeout("UpdateClock()", 500);
  }

  function KillClock() {
     if(clockID) {
        clearTimeout(clockID);
        clockID  = 0;
     }
  }
  StartClock();
</script>

現在のブラウザのタイムゾーンオフセットがブラウザの時間に追加され、次に目的のタイムゾーンオフセットが差し引かれ、必要な時間が取得されます。

これを機能させるには、必要なタイムゾーンを何らかの方法でブラウザに伝達する必要があります。

于 2013-01-08T08:14:02.763 に答える