有人可以解释这个奇怪的 javascript 日期输出吗?
Can someone explain this odd javascript date output?
https://jsfiddle.net/8cvhnwgs/1/
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
$("#out").html(new Date(2015, 5, 31, 08, 25, 30, 0));
我给的日期是5月31日,但是输出的是7月1日,有人能解释一下为什么吗?我用错参数了吗?
Date
构造函数的 month
参数是 0 索引,所以 5 是六月,只有 30 天。
看看Date。月份是从 0 到 11 的整数值。
new Date(2015, 0, 31)
// Sat Jan 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 1, 31)
// Tue Mar 03 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 2, 31)
// Tue Mar 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 3, 31)
// Fri May 01 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 4, 31)
// Sun May 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 5, 31)
// Wed Jul 01 2015 00:00:00 GMT+0000 (UTC)
试试这个:
$("#out").html(new Date(2015, 4, 31, 08, 25, 30, 0));
5是6月,因为6月没有31,所以到7月1日。
https://jsfiddle.net/8cvhnwgs/1/
//new Date(year, month, day, hours, minutes, seconds, milliseconds)
$("#out").html(new Date(2015, 5, 31, 08, 25, 30, 0));
我给的日期是5月31日,但是输出的是7月1日,有人能解释一下为什么吗?我用错参数了吗?
Date
构造函数的 month
参数是 0 索引,所以 5 是六月,只有 30 天。
看看Date。月份是从 0 到 11 的整数值。
new Date(2015, 0, 31)
// Sat Jan 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 1, 31)
// Tue Mar 03 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 2, 31)
// Tue Mar 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 3, 31)
// Fri May 01 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 4, 31)
// Sun May 31 2015 00:00:00 GMT+0000 (UTC)
new Date(2015, 5, 31)
// Wed Jul 01 2015 00:00:00 GMT+0000 (UTC)
试试这个:
$("#out").html(new Date(2015, 4, 31, 08, 25, 30, 0));
5是6月,因为6月没有31,所以到7月1日。