写了代码,一切正常,但在我看来,它可以更好地优化。
const [working, setWorking] = useState(true)
const workTime = () => {
const simpleNotWorking = [6, 7]
const weekendNotWorking = [6, 7, 8, 9, 10]
const currentDate = new Date()
const currentTime = currentDate.getHours()
const currentDay = currentDate.getDay()
if (currentDay >= 1 && currentDay <= 5) {
if (simpleNotWorking.includes(currentTime))
setWorking(false)
}
if (currentDay === 6 || currentDay === 7) {
if (weekendNotWorking.includes(currentTime)) {
setWorking(false)
}
}
}
底线是,有一家餐厅,工作日从早上 8 点工作到第二天早上 6 点(工作日只有 2 小时从早上 6 点到早上 8 点不工作)周末他们从早上 11 点到早上 6 点工作)
我制作了 2 个数组,我在其中指出了餐厅不工作的位置。我还通过 getDay (1-7) 获取星期几,并通过通常的检查
如何缩短代码?
唯一想到的:
PS getDay 周日不返回 0 吗?
你可以看起来像这样。