如何使今天的日期等于特定日期?
How do I make today's date equal to a specific date?
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');
上面的代码 returns 正常的一天。我希望它是真的。
您可以对两个值调用 .getTime(),然后对数值进行比较。
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');
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');
上面的代码 returns 正常的一天。我希望它是真的。
您可以对两个值调用 .getTime(),然后对数值进行比较。
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');