Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript date comparison [duplicate]

Tags:

Possible Duplicate:
Compare 2 dates with JavaScript

I haven't done much JavaScript. I am trying to compare two dates. From jconsole:

a = ["01/01/2010","01/02/2010","01/03/2010"]

date1 = new Date('01/02/2010')
Sat Jan 02 2010 00:00:00 GMT-0800 (PST)

date2 = new Date(a[1])
Sat Jan 02 2010 00:00:00 GMT-0800 (PST)

date1 == date2
false

Can someone tell me why this does not match?

like image 337
user290870 Avatar asked May 02 '10 05:05

user290870


1 Answers

Your comparison is returning false because date1 and date2 are simply references to different objects, and you are actually comparing these references.

To do a proper comparison of the date values, you can use the getTime() method as follows:

date1.getTime() === date2.getTime();   // returns true
like image 154
Daniel Vassallo Avatar answered Oct 25 '22 13:10

Daniel Vassallo