我需要得到两个日期和时间之间的秒差。
我有这个脚本:
var date1 = new Date(2013,10,02,12,00,00);
var date2 = new Date(2013,10,02,12,01,00);
var diff = date2 - date1;
diff = diff / 1000;
document.write(diff);
其中返回值60,相差60秒,不错。
但是,当我跨越一个 30 天的月份时,它计算不正确。
var date1 = new Date(2013,9,30,12,00,00);
var date2 = new Date(2013,10,02,12,00,00);
var diff = date2 - date1;
diff = diff / 1000;
document.write(diff);
返回的结果是 259200,也就是 3 天。9 月 30 日和 10 月 2 日相差只有 2 天,172800,因为一个月只有 30 天。为什么 Javascript 似乎认为 9 月有 31 天?