Javascript date comparison ignoring timestamp value

What is the easiest way to compare two dates, ignoring timestamps. I have two dates firstDatecoming from a database (converted to javascript date) and secondDatecoming from a date input field.

 Essentially for my algorithm, these two dates are equal, since it does not take the timestamp into account, but the code will not consider it equal and firstDatehas 01:00:00it.

How to completely abandon the timestamp for comparison?

firstDate:

Tue Mar 24 1992 01:00:00 GMT-0500 (Central Daylight Saving Time)

secondDate:

Tue Mar 24 1992 00:00:00 GMT-0500 (Central Daylight Saving Time)

The code:

   if(firstDate < secondDate) {

        alert("This alert shouldn't pop out as dates are equal");
    }
+5
source share
3 answers

toDateString, firstDate secondDate. , , , .

firstDate = new Date(firstDate.toDateString());
secondDate = new Date(secondDate.toDateString());
if(firstDate < secondDate){
    alert("This alert shouldn't pop out as dates are equal");
}

, , , , , . - .

firstDate.valueOf() == secondDate.valueOf()

JSFiddle.

+5

Date.setHours(hrs, mins, secs, ms) , ( , ).

+3

. - RFC3339.

var firstDateStr = firstDate.toISOString().slice(0, 10);
var secondDateStr = secondDate.toISOString().slice(0, 10);

return (firstDateStr < secondDateStr);
0

All Articles