Last active
July 31, 2023 04:22
-
-
Save kidGodzilla/ac50551bd56c1bd2ce2232538b6458c5 to your computer and use it in GitHub Desktop.
Is currently during working hours (Javascript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example config for 9-5, M-F, PST (US) | |
var config = { | |
0: { | |
open: 1, | |
close: 0 | |
}, | |
1: { | |
open: 9, | |
close: 17 | |
}, | |
2: { | |
open: 9, | |
close: 17 | |
}, | |
3: { | |
open: 9, | |
close: 17 | |
}, | |
4: { | |
open: 9, | |
close: 17 | |
}, | |
5: { | |
open: 9, | |
close: 17 | |
}, | |
6: { | |
open: 1, | |
close: 0 | |
}, | |
offset: -7 | |
}; | |
Date.prototype.subHours = function (h) { | |
this.setTime(this.getTime() - (h * 60 * 60 * 1000)); | |
return this; | |
} | |
function isWorkingHour (now) { | |
// Adjust remote user's time to match business time | |
now = now.subHours(config.offset + (now.getTimezoneOffset() / 60)); | |
var day = now.getDay(); | |
var hours = now.getHours(); | |
// console.log(day, hours, config[day].open, config[day].close, config.offset); | |
return hours >= config[day].open && hours < config[day].close; | |
} | |
console.log(isWorkingHour(new Date())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an adaptation, that adds support for 'exception' days that may have different working hours, with also support for MINUTES in working hour durations (so you can say the business is open from 9:30am instead of 9am):