2017-08-21 28 views
0

我倒數日食使用jquery countdown.js。我的桌面上的一切都正確計數。在移動設備(android)上,倒計時提前一個小時。在其他人的手機上,倒計時顯示0小時。我究竟做錯了什麼?我沒有考慮時區嗎?應該這樣嗎?倒計時是對月食(從現在開始不到4小時),所以我可以很快使用幫助!javascripts/jquery倒計時在手機上給出錯誤的小時數

var date = new Date("2017-08-21T13:18:00"); //1:18 p.m. EST 

$("#dayElem") 
    .countdown(date, function(event) { 
    if (event.strftime('%-D') < 10) { 
     $(this).text('0' + 
      event.strftime('%-D') 
     ); 
    } else { 
     $(this).text(
      event.strftime('%-D') 
     ); 
    } 
    }); 

    $("#hourElem") 
    .countdown(date, function(event) { 
    if (event.strftime('%-H') < 10) { 
     $(this).text('0' + 
      event.strftime('%-H') 
     ); 
    } else { 
     $(this).text(
      event.strftime('%-H') 
     ); 
    } 
    }); 
    $("#minuteElem") 
    .countdown(date, function(event) { 
    if (event.strftime('%-M') < 10) { 
     $(this).text('0' + 
      event.strftime('%-M') 
     ); 
    } else { 
     $(this).text(
      event.strftime('%-M') 
     ); 
    } 
    }); 
    $("#secondElem") 
    .countdown(date, function(event) { 

    if (event.strftime('%-S') < 10) { 
     $(this).text('0' + 
      event.strftime('%-S') 
     ); 
     } else { 
     $(this).text(
      event.strftime('%-S') 
     ); 
     } 

    }); 

回答

0

我認爲你的假設是正確的。你必須考慮當前的時區。 做到這一點,最簡單的方法是使用moment.js(https://momentjs.com/)和修改這樣的代碼:

var date = moment.utc('2017-08-21T17:18:00').toDate(); 

$("#dayElem") 
     .countdown(date, function (event) { 
      if (event.strftime('%-D') < 10) { 
       $(this).text('0' + 
         event.strftime('%-D') 
         ); 
      } else { 
       $(this).text(
         event.strftime('%-D') 
         ); 
      } 
     }); 

$("#hourElem") 
     .countdown(date, function (event) { 
      if (event.strftime('%-H') < 10) { 
       $(this).text('0' + 
         event.strftime('%-H') 
         ); 
      } else { 
       $(this).text(
         event.strftime('%-H') 
         ); 
      } 
     }); 
$("#minuteElem") 
     .countdown(date, function (event) { 
      if (event.strftime('%-M') < 10) { 
       $(this).text('0' + 
         event.strftime('%-M') 
         ); 
      } else { 
       $(this).text(
         event.strftime('%-M') 
         ); 
      } 
     }); 
$("#secondElem") 
     .countdown(date, function (event) { 

      if (event.strftime('%-S') < 10) { 
       $(this).text('0' + 
         event.strftime('%-S') 
         ); 
      } else { 
       $(this).text(
         event.strftime('%-S') 
         ); 
      } 

     }); 

另外請注意,我指定的UTC而不是EST的時間。

在我的時區,CET中運行代碼時,我只有零。在使用上面的代碼時,我發佈時間約爲3小時10分鐘。