javascript日期不同天等于毫秒

时间:2012-06-19 16:04:27

标签: javascript

http://jsfiddle.net/skowron_line/zPCBc/1/

var d1 = '31.05.2012';
var d2 = '01.06.2012';

var s1 = d1.split('.');
var s2 = d2.split('.');

var nd1 = new Date(s1[2], s1[1], s1[0]);
var nd2 = new Date(s2[2], s2[1], s2[0]);

$('#a').html(s1 + ' - '+ s2 +' = '+ nd2.getTime() +' - '+ nd1.getTime());

$('#b').html(
nd1.getFullYear() +'-'+ nd1.getMonth() +'-'+ nd1.getDate() +'<br />'+ nd2.getFullYear() +'-'+ nd2.getMonth() +'-'+ nd2.getDate()
);

有人可以解释我的代码有什么问题吗?为什么31.05.2012我等于01.06.2012

1 个答案:

答案 0 :(得分:6)

Javascript月份基于0,因此05月实际上是6月份。由于没有6月31日,JS正在将日期调整为7月(06年6月)。

新的日期代码应为:

var nd1 = new Date(s1[2], parseInt(s1[1])-1, s1[0]);