-3

I am having an issue with a javascript date comparison. I can output the objects in the console and everything looks right, but they always evaluate to false.

var date1 = new Date('2013','01','01'); 
var date2 = new Date('2015','01','01'); 
console.log("date1=" + date1);
console.log("date2=" + date2);
console.log("date1 > date2" + date1 > date2);
console.log("date1 < date2" + date1 < date2);

>>date1=Fri Feb 01 2013 00:00:00 GMT-0500 (EST)
>>date2=Sun Feb 01 2015 00:00:00 GMT-0500 (EST)
>>false
>>false 
user5045936
  • 83
  • 1
  • 2

1 Answers1

2

This is a matter of operator precedence:

Operator precedence determines the order in which operators are evaluated. Operators with higher precedence are evaluated first.

This makes a string with "date1 > date2" and date1 and compares it with date2:

"date1 > date2" + date1 > date2

This makes a string with "date1 > date2" and the comparison of date1 > date2:

"date1 > date2" + (date1 > date2)

Also you shouldn't compare date objects directly.

spenibus
  • 4,339
  • 11
  • 26
  • 35