I am trying to create a boolean function to check whether the current date/hour is a working hours. Now knowing that working hours are from 9AM to 5PM weekly except Fridays & Saturdays, I am facing issues with what I have e up with. I think my code works well for checking for days, but I just can't get it to work with hours as well. Here is my code:
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."
}
}
}
Here is my HTML Code test on JSFiddle:
/ Changing my PC clock works. My test case is a working day starting 5PM. And that's when the problem happens. The else block is not hit.
I am trying to create a boolean function to check whether the current date/hour is a working hours. Now knowing that working hours are from 9AM to 5PM weekly except Fridays & Saturdays, I am facing issues with what I have e up with. I think my code works well for checking for days, but I just can't get it to work with hours as well. Here is my code:
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."
}
}
}
Here is my HTML Code test on JSFiddle:
http://jsfiddle/jp9BW/ Changing my PC clock works. My test case is a working day starting 5PM. And that's when the problem happens. The else block is not hit.
Share Improve this question edited Oct 5, 2013 at 11:15 FrengiX asked Oct 5, 2013 at 10:06 FrengiXFrengiX 771 silver badge7 bronze badges 7- The problem now is that the code I posted gets the days right. So if it is not a working Day things go right. However, the problem is when the time is out of working hours. So if it is a working day and not a working hour, the else statement is not hit. – FrengiX Commented Oct 5, 2013 at 10:31
- I don't see the problem with your code. Works fine for me. Maybe you have a problem elsewhere? – eis Commented Oct 5, 2013 at 10:41
- Try to set the time to be Monday @7PM.. My else statement is not hit in this case. Working hours are Sundays through Thursdays 9AM-5PM – FrengiX Commented Oct 5, 2013 at 10:44
- @FrengiX what does now.getHours() give for you in that case? – eis Commented Oct 5, 2013 at 10:50
- Monday @7PM gives me now.getHours() gives me "19" – FrengiX Commented Oct 5, 2013 at 11:01
2 Answers
Reset to default 6I believe, this should be enough
function isWorkingHour(now) {
return now.getDay() <= 4 && now.getHours() >= 9 && now.getHours() < 17;
}
I would do it like this
function checkOpeningTimes() {
let date = new Date(); // current time
let hours = date.getHours();
let day = date.getDay();
let openingDays = [ 0, 1, 2, 3, 4 ];
return openingDays.includes( day ) && hours >= 9 && hours <= 17;
}