我正在尝试创建一个 bool 函数来检查当前日期/时间是否为工作时间。现在知道工作时间是每周上午 9 点到下午 5 点,周五和周六除外,我面临着我所想出的问题。我认为我的代码可以很好地检查几天,但我也不能让它在几个小时内工作。这是我的代码:
var dayOfWeek = now.getDay();
//weekday 0==Sunday 1==Monday 2==Tuesday 3==Wednesday 4==Thurs 5==Friday 6==Sat
//Not Friday or Saturday
if ((dayOfWeek != 5) && (dayOfWeek != 6)){
if (now.getHours() >= 9 && now.getHours() < 17 ) {
//Within working hours now."
}
else
{
//After working hours."
}
}
}
这是我在 JSFiddle 上的 HTML 代码测试:
http://jsfiddle.net/jp9BW/ 更改我的 PC 时钟有效。我的测试用例是从下午 5 点开始的工作日。那就是问题发生的时候。 else block 未命中。
最佳答案
我相信,这应该足够了
function isWorkingHour(now) {
return now.getDay() <= 4 && now.getHours() >= 9 && now.getHours() < 17;
}
关于javascript - 检查工作时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19196490/