1 回答

TA貢獻1824條經(jīng)驗 獲得超5個贊
這是我在評論中建議的:
const msPerDay = 24 * 60 * 60 * 1000;
window.addEventListener("load", function(){
var newButton = document.getElementById("newButton");
const start = 12 * 60 + 30;
const end = 13 * 60 + 30;
const date = new Date();
const now = date.getHours() * 60 + date.getMinutes();
if(start <= now && now <= end
&& Math.round((date - 1606798800000) / msPerDay) % 14 === 0) {
newButton.style.display = "block";
alert("in time");
}
else {
newButton.style.display = "none";
alert("offline");
}
}, false);
請注意問題中有趣的歧義。“檢查日期是否是從 12 月 1 日開始的每個第二個星期二?” 可以按照預(yù)期讀作代表交替的星期二。但我最初的誤讀“任何一個月的第二個星期二”是完全可以理解的,如果考慮到所指出的開始日期本身就是星期二,我認為可能性稍小一些。
1606798800000僅僅是 的結(jié)果new Date('2020-12-01').getTime()。不簡單地在代碼中包含類似的內(nèi)容可能是一個愚蠢的優(yōu)化。所以替代版本可能是
if (start <= now && now <= end &&
Math.round((date - new Date(2020, 11, 1).getTime()) / msPerDay) % 14 === 0)
添加回答
舉報