Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Date Comparisons Don't Equal [duplicate]

I've tried searching for people with similar questions, but haven't found anything.

I have two dates in JavaScript, both set to the same value... Equality Testing fails on ==, but >= and <= evaluate true.

Below is the code I have in play:

var startDate = new Date( 2011, 7, 30, 0, 0, 0, 0 );  var dt = new Date( 2011, 7, 30, 0, 0, 0, 0 );  if( startDate == dt )     document.write('They Equal<br />');  if( startDate > dt )     document.write('Start Date is > dt<br />');  if( startDate >= dt )     document.write('Start Date is >= dt<br />');  if( startDate < dt )     document.write('Start Date is < dt<br />');  if( startDate <= dt )     document.write('Start Date is <= dt<br />');  if( dt == startDate )     document.write('They Equal<br />');  if( dt > startDate )     document.write('dt > startDate<br />');  if( dt >= startDate )     document.write('dt >= Start Date <br />');  if( dt < startDate )     document.write('dt < Start Date <br />');  if( dt <= startDate )     document.write('dt <= Start Date <br />');    document.write( dt ); document.write( '<br />'); document.write( startDate ); 

Has anyone encountered anything like this, or am I doing something fundamentally wrong?

I tested this is Internet Explorer (9), Firefox 5+, and Chrome.

Update:

So two people posted great answers to my problem, and I thank both of you: xdazz and DaveRandom. I had read an earlier post on stackoverflow.com on a similar question and a guy said that date objects could be compared like any others, and any other example I found always did a < or > type of compare, never a full equality so I wasn't able to make the connection as to why I was doing it wrong.

Thanks to you two, and the others that posted similar answers.

like image 366
Patrick Avatar asked Aug 30 '11 14:08

Patrick


People also ask

Why does JavaScript use === instead of ==?

Use === if you want to compare couple of things in JavaScript, it's called strict equality, it means this will return true if only both type and value are the same, so there wouldn't be any unwanted type correction for you, if you using == , you basically don't care about the type and in many cases you could face ...

How do you compare two date objects in JavaScript?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.

What does JavaScript use instead of equal to and not equal to?

The JavaScript not equal or inequality operator (!=) checks whether two values are not equal and returns a boolean value. This operator tries to compare values irrespective of whether they are of different types. However, the “!==


Video Answer


2 Answers

I think if you do

var startDate = (new Date( 2011, 7, 30, 0, 0, 0, 0 )).getTime(); var dt = (new Date( 2011, 7, 30, 0, 0, 0, 0 )).getTime(); 

At the top of the script you will find it works.

The getTime() method returns the date as an integer, what you are doing there is comparing objects, rather than specific values.

EDIT Fixed above code

like image 24
DaveRandom Avatar answered Oct 29 '22 11:10

DaveRandom


When you use <= or >= to compare two date objects, they are compared via valueOf, which is the same as getTime for Date.

But when you use ==, they are two different objects of the same type, so it returns false.

Added some examples:

> new Date(2011, 7, 30, 0, 0, 0, 0) == new Date( 2011, 7, 30, 0, 0, 0, 0 ) false > new Date(2011, 7, 30, 0, 0, 0, 0).getTime() == new Date( 2011, 7, 30, 0, 0, 0, 0).getTime() true > new Date(2011, 7, 30, 0, 0, 0, 0).valueOf() == new Date( 2011, 7, 30, 0, 0, 0, 0).valueOf() true > new Date(2011, 7, 30, 0, 0, 0, 0).valueOf() == new Date( 2011, 7, 30, 0, 0, 0, 0).getTime() true 
like image 74
xdazz Avatar answered Oct 29 '22 11:10

xdazz