JavaScript Date Object Comparison

JavaScript Date Object Comparison

That is because in the second case, the actual date objects are compared, and two objects are never equal to each other. Coerce them to number:

 alert( +startDate2 == +startDate3 ); // true

If you want a more explicity conversion to number, use either:

 alert( startDate2.getTime() == startDate3.getTime() ); // true

or

 alert( Number(startDate2) == Number(startDate3) ); // true

Oh, a reference to the spec: §11.9.3 The Abstract Equality Comparison Algorithm which basically says when comparing objects, obj1 == obj2 is true only if they refer to the same object, otherwise the result is false.

Compare two dates with JavaScript

The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.

The ==, !=, ===, and !== operators require you to use date.getTime() as in

var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();

to be clear just checking for equality directly with the date objects won't work

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2); // prints false (wrong!)
console.log(d1 === d2); // prints false (wrong!)
console.log(d1 != d2); // prints true (wrong!)
console.log(d1 !== d2); // prints true (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.


For the curious, date.getTime() documentation:

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)

Comparing date part only without comparing time in JavaScript

I'm still learning JavaScript, and the only way that I've found which works for me to compare two dates without the time is to use the setHours method of the Date object and set the hours, minutes, seconds and milliseconds to zero. Then compare the two dates.

For example,

date1 = new Date()
date2 = new Date(2011,8,20)

date2 will be set with hours, minutes, seconds and milliseconds to zero, but date1 will have them set to the time that date1 was created. To get rid of the hours, minutes, seconds and milliseconds on date1 do the following:

date1.setHours(0,0,0,0)

Now you can compare the two dates as DATES only without worrying about time elements.

how to compare two string dates in javascript?

var d1 = Date.parse("2012-11-01");
var d2 = Date.parse("2012-11-04");
if (d1 < d2) {
alert ("Error!");
}

Demo Jsfiddle

Recently found out from a comment you can directly compare strings like below

if ("2012-11-01" < "2012-11-04") {
alert ("Error!");
}

How to tell if two dates are in the same day or in the same hour?

The Date prototype has APIs that allow you to check the year, month, and day-of-month, which seems simple and effective.

You'll want to decide whether your application needs the dates to be the same from the point of view of the locale where your code runs, or if the comparison should be based on UTC values.

function sameDay(d1, d2) {
return d1.getFullYear() === d2.getFullYear() &&
d1.getMonth() === d2.getMonth() &&
d1.getDate() === d2.getDate();
}

There are corresponding UTC getters getUTCFullYear(), getUTCMonth(), and getUTCDate().

Why Date comparison is showing weird behavior in javascript?

Use Date.getTime to compare the timestamp, otherwise you are simply comparing the objects, which we know aren't the same.

var d1 = new Date(),  d2 = new Date();
function fullCompare(a, b) { console.log(a == b, a <= b, a >= b, a < b, a > b);}fullCompare(d1, d2);fullCompare(d1.getTime(), d2.getTime());

How does Javascript string compare work with dates?

Strings are always compared character by character.

var bigger = "2017-01-01";var smaller = "2000/01/01";
console.log(`'${bigger}' > '${smaller}'`, bigger > smaller);
bigger = "2000-12-01";console.log(`'${bigger}' > '${smaller}'`, bigger > smaller);


Related Topics



Leave a reply



Submit