LoginSignup
3
3

More than 5 years have passed since last update.

カウントダウンタイマーを作るときに、new Dateで作った日付の数値の計算でIEが「NaN」を返してバグる件-その回避方法

Posted at

カウントダウンタイマーを作っていたら、IEのみNaNを返して困っていたが、単純なミスだった。

件のカウントダウンタイマーは下記

coountdown.js
<script type="text/javascript">
  $(function() {
    countDown();
    });
    function countDown() {
    var startDateTime = new Date();
    var endDateTime = new Date("8 26,2011 11:00:00");
    var d = Math.floor((endDateTime-startDateTime)/(24*60*60*1000))
    var h = Math.floor(((endDateTime-startDateTime)%(24*60*60*1000))/(60*60*1000))
    var m = Math.floor(((endDateTime-startDateTime)%(24*60*60*1000))/(60*1000))%60
    var s = Math.floor(((endDateTime-startDateTime)%(24*60*60*1000))/1000)%60%60
    $("#TimeLeft").text(d+''+h+'時間'+m+''+s+'');
      setTimeout('countDown()', 1000);
    }
</script>

引用元はこちら
グルーポン系サイトでよく見掛けるカウントダウンタイマーのサンプルコード

このコードは,Firefoxだと動くが、IEだと動かない。
各数値でNaNが返る。

引用元のコードと違う点はたったひとつで、
endDateTimeのnew Date()にて、[月]を数字で指定していること。

var endDateTime = new Date("8 26,2011 11:00:00");

どうやらこれが原因らしい。
バグを回避するには、きちんと英語にて[月]を記入する必要がある。

var endDateTime = new Date("August 26,2011 11:00:00");

たったこれだけの話。
めんどくさがって(英語力が足りないからといって)数字で入力、ダメゼッタイ。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3