1

each element contains the same value. When checked against this value by themselves, everything checks out. But when compared against eachother, they are not shown as being equal. help! thanks!

time[x] == "2013-02-26 14:00:00"   ?

true

reference[x] == "2013-02-26 14:00:00"  ?

true

time[x] == reference[x]  ?

false

time[x].valueOf() == reference[x].valueOf()  ?

false

4
  • 3
    Can you include your full code? Commented Feb 26, 2013 at 21:29
  • 1
    have you tried just comparing strings? time[x].toString() == reference[x].toString()? Commented Feb 26, 2013 at 21:30
  • toString() is the way to go. time[x].toString() == reference[x].toString() returns true, when those two values are the same. Rock! thanks! Commented Feb 26, 2013 at 21:40
  • I'm guessing you have two objects (and two objects are never equal). Maybe even two String objects, created with new String("2013-02-26 14:00:00"); Commented Feb 26, 2013 at 21:47

1 Answer 1

3

This might happen because the two variables are of different types.

In case one of the variables holds a Date instance and the other a String, comparing both of them to a string literal will return true, while comparing their valueOf() results will return false, since valueOf() of a Date returns number of milliseconds since epoch, not the human-readable representation of a date (as opposed to toString()).

var a = new Date()
a.toString() //"Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
a.valueOf()  //1361918511306

var b = "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
b.toString() //"Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
b.valueOf()  //"Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"

a == "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)" //true
b == "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)" //true
a == b //true
a === b //false - types are being compared as well
a.valueOf() == b.valueOf() //false - 1361918511306 compared to "Wed Feb 27 2013 01:41:51 GMT+0300 (MSK)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.