0

特定の日付までの正確な秒数、分数などを取得しようとしています。これはばかげているように聞こえるかもしれませんが、なぜ結果が 2 倍になるのでしょうか? それは正しくないようですね。

setInterval(function() {
    var startDate = new Date(),
        startDateTime = startDate.getTime(),
        endDate = new Date(2012, 5, 14),
        endDateTime = endDate.getTime();
    var timeLeft = endDateTime - startDateTime;
    var seconds = Math.round(timeLeft / 1000),
        minutes = Math.round(seconds / 60),
        hours = Math.round(minutes / 60),
        days = Math.round(hours / 24),
        weeks = Math.round(days / 7);
    console.log(weeks, days, hours, minutes, seconds);
}, 1000);​
4

1 に答える 1

4

JavaScript の Date コンストラクターは、Java の Date コンストラクターと同じように機能します。日と年は 1 から始まりますが、月は 0 から始まります。したがって、2015 年 2 月 10 日の日付は次のようになります。

var aDate= new Date(2015,1,10);
于 2012-04-16T19:49:52.863 に答える