1var date1 = new Date('December 25, 2017 01:30:00');
2var date2 = new Date('June 18, 2016 02:30:00');
3
4//best to use .getTime() to compare dates
5if(date1.getTime() === date2.getTime()){
6 //same date
7}
8
9if(date1.getTime() > date2.getTime()){
10 //date 1 is newer
11}
1var date1 = new Date('December 25, 2017 01:30:00');
2var date2 = new Date('June 18, 2016 02:30:00');
3
4//best to use .getTime() to compare dates
5if(date1.getTime() === date2.getTime()){
6 //same date
7}
8
9if(date1.getTime() > date2.getTime()){
10 //date 1 is newer
11}
1var d1 = Date.parse("2012-11-01");
2var d2 = Date.parse("2012-11-04");
3if (d1 < d2) {
4 alert ("Error!");
5}
1var x = new Date('2013-05-23');
2var y = new Date('2013-05-23');
3
4// less than, greater than is fine:
5x < y; => false
6x > y; => false
7x === y; => false, oops!
8
9// anything involving '=' should use the '+' prefix
10// it will then compare the dates' millisecond values
11+x <= +y; => true
12+x >= +y; => true
13+x === +y; => true
14
1let myDate = new Date("January 13, 2021 12:00:00");
2let yourDate = new Date("January 13, 2021 15:00:00");
3
4if (myDate < yourDate) {
5 console.log("myDate is less than yourDate"); // will be printed
6}
7if (myDate > yourDate) {
8 console.log("myDate is greater than yourDate");
9}