1
var currentDate = Thu Aug 14 2014 05:30:00 GMT+0530 (IST); // This is dateTimeobj

var deptDate = Thu Aug 14 2014 14:23:24 GMT+0530 (IST); // This is dateTimeobj

alert(currentDate == deptDate); // false

How can I compare these two dates, while ignoring the time portion?

nbrooks
  • 18,126
  • 5
  • 54
  • 66
user3676578
  • 213
  • 1
  • 5
  • 17
  • 1
    This question is not a duplicate of either of the proposed originals, as it's clearly requesting a specific type of comparison. – nbrooks Aug 14 '14 at 09:43

2 Answers2

3

First things first: date objects have nothing to do with jQuery, that is vanilla JavaScript.

Now, assuming currentDate and deptDate are Date objects, set both dates to midnight with setHours() and then compare:

currentDate = currentDate.setHours(0,0,0,0);
deptDate = deptDate.setHours(0,0,0,0);

var check = currentDate == deptDate;
George
  • 36,413
  • 9
  • 66
  • 103
0

You can call the following function with both dates as parameters, for comparing by year, month, and date:

function areDatesEqual(date1, date2) {
    return (date1.getUTCDate() == date2.getUTCDate() 
        && date1.getUTCMonth() == date2.getUTCMonth() 
        && date1.getUTCFullYear() == date2.getUTCFullYear());
}

alert(areDatesEqual(currentDate, deptDate));

Thanks to david a. for the timezone suggestion. See jsFiddle here.

nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • 1
    Depending on actual requirement, timezones might matter as well. If yes, I'd use `getUTCDate()`, `getUTCMonth()`, `getUTCFullYear()` instead. I would do this if it was required to check whether the dates represents a time frame that starts/ends at the same instant, no matter where my user(s) are. – david a. Aug 14 '14 at 09:18
  • @davida. Thanks for the suggestion. This probably wouldn't typically be an issue since JS dates would be handled in the client's timezone, but there might have been issues going from encoded strings back to date objs. Updated accordingly. – nbrooks Aug 14 '14 at 09:33