Skip to content Skip to sidebar Skip to footer

Comparing Javascript Dates While Ignoring Time Of Day

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

Solution 1:

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;

Solution 2:

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

functionareDatesEqual(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.

Post a Comment for "Comparing Javascript Dates While Ignoring Time Of Day"