为什么这不正确?Javascript日期比较错误?
function test() {
var start = new Date(2012, 3, 31, 19, 0, 0); // 3/31/2012 7:00 PM
var end = new Date(2012, 4, 1, 1, 0, 0); // 4/01/2012 1:00 AM
if (end < start)
console.log("oops!");
else
console.log("works!");
}
输出:
糟糕!
[JavaScript Date Object Comparison]的可能重复(http://stackoverflow.com/questions/7606798/javascript-date-object-comparison) – 2012-03-23 17:11:28
为什么这是一个错误?您的开始实际上是在您创建完日期后给出的。 3是4月,只有30天,但你已经将日期设置为31,这使得5月。几个月使用0索引。 – kinakuta 2012-03-23 17:12:25
@AdrianIftode - 不,3是4月份 - 月份为0。 – 2012-03-23 17:25:03