1

I have two javascript dates, dt and Frefdate. In the firebug, both value are Date {Fri Apr 08 2005 00:00:00 GMT+1000 (AUS Eastern Summer Time)}.

But dt == Frefdate is false. I have to use dt.valueOf() == Frefdate.valueOf() to compare these two dates. Why?

in w3schools web site, the dates comparison can be date1 > date2.(see http://www.w3schools.com/js/js_obj_date.asp).

Thank you.

2
  • 2
    Two dates are two different objects. Commented Feb 20, 2014 at 3:54
  • It's false for the same reason {} == {} is false: They are different objects. .valueOf returns a number and two numbers are equal if they have the same value. Commented Feb 20, 2014 at 3:57

2 Answers 2

1

When you compare two different date objects, you aren't comparing their value, you're comparing that they are in fact the same object.

Even if the two date objects are set to the same date and time, they are two separate instances. Think of it like comparing references to a structure in memory. The memory location for each is going to be different.

Sign up to request clarification or add additional context in comments.

Comments

0

The two dates are two different objects, so they are not "object-equal". JavaScript lets you compare strings and numbers using ==, but all other types are compared as objects.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.