The problem with your code is that you are comparing time / date, not just the date.
Try this code:
var myDate = new Date(); myDate.setDate(17); myDate.setMonth(7); myDate.setYear(2009); //Delay code - start var total = 0; for(var i=0; i<10000;i++) total += i; //Delay code - end var now = new Date(); console.log(now.getTime() == myDate.getTime());
If you save the loop cycle code (identified by the “Start-delay code”), the console will show “false”, and if you delete the loop cycle code, the console will write “true”, although in both cases myDate is 7/17/2009 and “now "- 7/17/2009.
The problem is that the JavaScript date object stores both date and time . If you want to compare only the date, you need to write code.
function areDatesEqual(date1, date2) { return date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate(); }
This function will print 'true' if the two parts of the javascript date are equal, ignoring the associated time part.
source share