0

I have 2 date objects like the following:

var d1 = new Date(2017, 01, 11, 20, 59, 59);
var d2 = new Date(2017, 01, 11, 22, 59, 59);
d1 == d2 //false

In the example above d1 is not equal to d2

However I want to check the equality based on date, month and year without consideration of the time values.

Can anyone help me

ste-fu
  • 6,879
  • 3
  • 27
  • 46
pradeep
  • 117
  • 1
  • 8
  • duplicate of http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Pierre Emmanuel Lallemant Jan 11 '17 at 10:08
  • 1
    Possible duplicate of [Comparing date part only without comparing time in JavaScript](http://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript) – Robby Cornelissen Jan 11 '17 at 10:08
  • http://stackoverflow.com/questions/7606798/javascript-date-object-comparison I believe the question above is able to help :) – Hugo Torres Jan 11 '17 at 10:08
  • the problem its not give you `true` is because `d1` and `d2` have a slightly difference time in miliseconds, you need to compare just the date without it milliseconds – rudydydy Jan 11 '17 at 10:20

5 Answers5

2

You can access date, month and year values on the Date object separately (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). That allows you to specify your own comparison function based on equality of your specific date fields.

For the case you are describing, simple d1.toDateString() === d2.toDateString() would do.

Petr Volny
  • 539
  • 3
  • 12
  • @RobG I agree but in my answer I am not suggesting usage of `toString` but `toDateString` which returns only human readable *date* portion (year, month, day) of the Date so times are omitted in this representation thus the two dates (with different hours/minutes or seconds) equals and hence it imho is what OP wants ;) – Petr Volny Jan 12 '17 at 15:48
  • Yes, my mistake. ;-) – RobG Jan 12 '17 at 20:17
1

Try This by creating one function that return date as MM/DD/YYYY format by excluding time like below:

function GetFormattedDate(dateObj) {

    var dt = dateObj
    var d = dt.getDate();
    var dd = d < 10 ? "0" + d : d;
    var m = dt.getMonth() + 1;
    var mm = m < 10 ? "0" + m : m;
    var y = dt.getFullYear();

    return mm + "/" + dd + "/" + y;

}

then use this function as:

var d1 = GetFormattedDate(new Date(2017, 01, 11, 20, 59, 59));
var d2 = GetFormattedDate(new Date(2017, 01, 11, 22, 59, 59));

if(d1 == d2)
   return true;
else
  return false;
Sagar Hirapara
  • 1,677
  • 13
  • 24
1

You can use the getTime function for that:

var d1 = new Date(2017, 01, 11, 23, 59, 59);
var d2 = new Date(2017, 01, 11, 22, 59, 59);

d1 = new Date(d1.getYear()+1900, d1.getMonth(), d1.getDate());
d2 = new Date(d2.getYear()+1900, d2.getMonth(), d2.getDate());

console.log(d1.getTime() === d2.getTime());//true since I changed the value of D1 and D2
Ankur Verma
  • 5,793
  • 12
  • 57
  • 93
  • Thank you can you explain the below code d1 = new Date(d1.getYear()+1900, d1.getMonth(), d1.getDate()); d2 = new Date(d2.getYear()+1900, d2.getMonth(), d2.getDate()); – pradeep Jan 12 '17 at 05:19
  • So, I didn't change the code you have given, since that object had time so I just picked the year, month and date from your object and reset the object, so that the equality will only check for the date part and not the time part, I hope it's clear. Also, +1900 is optional since the getYear() returns the years after 1900 so for this example it is not required but yeah if you are using d1 and d2 somewhere else please add 1900 to make it a normal year – Ankur Verma Jan 12 '17 at 07:12
0

Best way to match two dates is to convert them into time format and then match.

d1.getTime() === d2.getTime()

Hope it works for you.

Maharshi
  • 1,178
  • 1
  • 14
  • 37
  • This will also compare the time, so unless they are set to the same hour, minute, second and millisecond, they will not be equal. – RobG Jan 11 '17 at 12:30
  • @RobG considering this for both dates var d1 = GetFormattedDate(new Date(2017, 01, 11, 20, 59, 59)); var d2 = GetFormattedDate(new Date(2017, 01, 11, 22, 59, 59)); – Maharshi Jan 11 '17 at 12:34
0

A simple method is to copy the dates, set their time to zero and then compare time values:

function areEquivalentDates(d0, d1) {
  return new Date(d0).setHours(0,0,0,0) == new Date(d1).setHours(0,0,0,0);
}

// Test some dates
[[new Date(2017,1,23,0,0,0,1),
  new Date(2017,1,23,23,59,59,999)],
 [new Date(2017,1,23,12,0,0,1),
  new Date(2017,1,23,12,0,0,0)],
 [new Date(2017,1,23,23,59,59,999),
  new Date(2017,1,24,0,0,0,0)]
].forEach(a => console.log(a[0].toString() + '\n' +
                           a[1].toString() + '\n' + 
                           areEquivalentDates(a[0],a[1])
                          )
);
 
RobG
  • 142,382
  • 31
  • 172
  • 209