2
  const today = new Date();
  today.setHours(0,0,0,0); // Tue May 31 2022
 
  let dateHoliday = new Date('Tue May 31 2022');
  (today == dateHoliday) ? alert('holiday') : alert('regular day');

The code above returns regular day. I want it to be true.

edelcodes
  • 67
  • 7
  • 2
    You are testing two `objects` to see if they are the same one object, but they will never be. You need to be testing the values of the dates against each other. – Scott Marcus May 31 '22 at 18:38
  • `new Date('Tue May 31 2022')` is not a good idea, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) Far better to use `new Date(2022, 4, 31)` (which is also less to type). :-) – RobG Jun 01 '22 at 12:28

1 Answers1

2

You can call .getTime() on both values and then do a compare on the numeric values.

const today = new Date();
today.setHours(0,0,0,0); // Tue May 31 2022
 
let dateHoliday = new Date('Tue May 31 2022');
(today.getTime() === dateHoliday.getTime()) ? alert('holiday') : alert('regular day');
Bogus Hawtsauce
  • 363
  • 3
  • 10