JavaScript Date Comparisons Don't Equal

JavascriptDateEquality

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

This is one of the many illogical parts of Javascript. You can work around this problem by converting the date to a number using .getTime() that will return how many milliseconds passed from that date and 00:00:00 of Jan 1, 1970.

if (a.getTime() === b.getTime()) ...

The === operator on the objects themselves will return always false because those two objects are indeed distinct mutable javascript objects.

PS: Don't use == operator with Javascript. Ever. What it does takes the term "crazy" to a whole new level. Seriously. The problem is that applies all sort of type conversions that simply make no sense and for example you end up with things like "1"==1, x==false && (x?1:2)==1 or even a==b && b==c && a!=c being true (e.g. with x=[] or with a=[1], b=1, c=[[1]]). Just don't use ==. Ever. It's "FUBAR". Fullstop.

Solution 4 - Javascript

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

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

if( startDate.getTime() == dt.getTime() )
    console.log('They Equal<br />');

Solution 5 - Javascript

<Script>
function checkval()
{
var strfromdate=document.frmadmin.txtfromdate.value;
strfromdate=strfromdate.split("/");
//alert(strfromdate[2]+" "+strfromdate[0]+" "+strfromdate[1]);

var strtodate=document.frmadmin.txttodate.value;
strtodate=strtodate.split("/");

var fromDate = (new Date( strfromdate[2], strfromdate[0], strfromdate[1], 0, 0, 0, 0 )).getTime();
var toDate = (new Date( strtodate[2], strtodate[0], strtodate[1], 0, 0, 0, 0 )).getTime();
//alert(fromDate+" "+toDate);

	
	
	if(toDate<fromDate)
	{
		alert("To date should be greater than from date.");
		document.frmadmin.txttodate.focus();
		return false;
	}
	
	
	return true;
}
</Script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatrickView Question on Stackoverflow
Solution 1 - JavascriptxdazzView Answer on Stackoverflow
Solution 2 - JavascriptDaveRandomView Answer on Stackoverflow
Solution 3 - Javascript6502View Answer on Stackoverflow
Solution 4 - JavascriptShefView Answer on Stackoverflow
Solution 5 - Javascriptuser3202283View Answer on Stackoverflow