Checking if two Dates have the same date info

JavascriptDateEquality

Javascript Problem Overview


How can I check if two different date objects have the same date information(having same day, month, year ...)? I have tried "==", "===" and .equals but none seems to work.

Javascript Solutions


Solution 1 - Javascript

You can use valueOf() or getTime():

a = new Date(1995,11,17);
b = new Date(1995,11,17);

a.getTime() === b.getTime() // prints true

Solution 2 - Javascript

If you are only interested in checking if dates occur on the same day regardless of time then you can use the toDateString() method to compare. This method returns only the date without time:

var start = new Date('2015-01-28T10:00:00Z');
var end = new Date('2015-01-28T18:00:00Z');

if (start.toDateString() === end.toDateString()) {
  // Same day - maybe different times
} else {
  // Different day
}

Solution 3 - Javascript

I used this code:

Date.prototype.isSameDateAs = function(pDate) {
  return (
    this.getFullYear() === pDate.getFullYear() &&
    this.getMonth() === pDate.getMonth() &&
    this.getDate() === pDate.getDate()
  );
}

Then you just call it like : if (aDate.isSameDateAs(otherDate)) { ... }

Solution 4 - Javascript

Type convert to integers:

a = new Date(1995,11,17);
b = new Date(1995,11,17);
+a === +b;  //true

Solution 5 - Javascript

Hellnar,

you could try (pardon the function name :) - amended per felix's valueof, rather than getTime)

function isEqual(startDate, endDate) {
    return endDate.valueOf() == startDate.valueOf();
}

usage:

if(isEqual(date1, date2)){
   // do something
}

might get you part of the way there.

see also:

'http://www.java2s.com/Tutorial/JavaScript/0240__Date/DatevalueOf.htm';

Solution 6 - Javascript

subtract them and compare to zero:

var date1 = new Date();
var date2 = new Date();

// do something with the dates...

(date1 - date2) ? alert("not equal") : alert("equal");

to put it into a variable:

var datesAreSame = !(date1 - date2);

Solution 7 - Javascript

For better date support use moment.js and isSame method

var starDate = moment('2018-03-06').startOf('day');
var endDate  = moment('2018-04-06').startOf('day');

console.log(starDate.isSame(endDate)); // false ( month is different )

var starDate = moment('2018-03-06').startOf('day');
var endDate  = moment('2018-03-06').startOf('day');

console.log(starDate.isSame(endDate)); // true ( year, month and day are the same )

Solution 8 - Javascript

A simple single line alternative for determining if two dates are equal, ignoring the time part:

function isSameDate(a, b) {
	return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}

It determines if dates a and b differ no more than one day and share the same day of the week.

function isSameDate(a, b) {
    return Math.abs(a - b) < (1000 * 3600 * 24) && a.getDay() === b.getDay();
}

console.log(isSameDate(new Date(2017, 7, 21), new Date(2017, 7, 21))); //exact same date => true
console.log(isSameDate(new Date(2017, 7, 21, 23, 59, 59), new Date(2017, 7, 21))); //furthest same dates => true
console.log(isSameDate(new Date(2017, 7, 20, 23, 59, 59), new Date(2017, 7, 21))); //nearest different dates => false
console.log(isSameDate(new Date(2016, 7, 21), new Date(2017, 7, 21))); //different year => false
console.log(isSameDate(new Date(2017, 8, 21), new Date(2017, 7, 21))); //different month => false

Solution 9 - Javascript

i like to use this code

function isEqualDate(d1, d2) {
    return (
        d1.toISOString().startsWith(d2.toISOString().substring(0,10)) 
    );
}

console.log(isEqualDate(new Date(2022,2,2,10,57), new Date(2022,2,2,11,57)) )

console.log(isEqualDate(new Date(), new Date(2022,2,2)) )

console.log(isEqualDate(new Date(), new Date()) )

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
QuestionHellnarView Question on Stackoverflow
Solution 1 - JavascriptFelix KlingView Answer on Stackoverflow
Solution 2 - JavascriptcyberwombatView Answer on Stackoverflow
Solution 3 - JavascriptIncidentlyView Answer on Stackoverflow
Solution 4 - Javascriptd2vidView Answer on Stackoverflow
Solution 5 - Javascriptjim tollanView Answer on Stackoverflow
Solution 6 - JavascriptEge ÖzcanView Answer on Stackoverflow
Solution 7 - Javascriptque1326View Answer on Stackoverflow
Solution 8 - JavascriptPhilip BijkerView Answer on Stackoverflow
Solution 9 - Javascripthossein sedighianView Answer on Stackoverflow