Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript date comparison bug?

Why doesn't this work right?

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!");
}

Output:

oops!

like image 524
peter Avatar asked May 08 '26 17:05

peter


1 Answers

Months are 0-based in js

var start = new Date(2012, 2, 31, 19, 0, 0); // 3/31/2012 7:00 PM
var end = new Date(2012, 3, 1, 1, 0, 0);     // 4/01/2012 1:00 AM

In your case both start and end are 1 May 2012. Just output the values and you'll see.

like image 67
Jakub Konecki Avatar answered May 10 '26 07:05

Jakub Konecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!