Last active
January 25, 2022 22:50
-
-
Save dudanogueira/8c94aaa20b9126c6c633ee9b3a7580d9 to your computer and use it in GitHub Desktop.
Simple business day checker
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
function isInRange(value, range) { | |
match = value >= range[0] && value <= range[1]; | |
return match | |
} | |
function isInRanges(value, range) { | |
matches = [] | |
range.split(",").forEach(function(timeframe) { | |
times = timeframe.split("-"); | |
match = isInRange(value, times) | |
matches.push(match) | |
}); | |
// return if any true | |
return matches.some(element => element) | |
} | |
// console.log( "SHOULD BE TRUE", | |
// isInRanges("06:30", "00:30-02:30,06:00-12:00,13:00-18:00") | |
// ) | |
// console.log( "SHOULD BE FALSE", | |
// isInRanges("06:30", "00:30-02:30,06:31-12:00,13:00-18:00") | |
// ) | |
function business_day(day, hours, today, rightnow){ | |
output = false | |
const d = new Date(); | |
// day is all or day if today | |
if(day == "*" || day == today){ | |
// hours is all or hours if today | |
if(hours == "*"){ | |
output = true | |
}else{ | |
if (hours == null){ | |
output = false | |
}else{ | |
output = isInRanges(rightnow, hours) | |
} | |
} | |
} | |
return output | |
} | |
matches = [] | |
const d = new Date(); | |
var today = d.getDay(); | |
console.log("TODAY", today) | |
console.log() | |
var time = new Date(); | |
var rightnow = time.getHours() + ":" + time.getMinutes() | |
console.log("RIGHTNOW", rightnow) | |
business_days = { | |
'2': '08:00-12:00,13:00-19:52', | |
'3': '*', | |
'4': null | |
} | |
for (i in business_days){ | |
check = business_day(i, business_days[i], today, rightnow); | |
matches.push(check) | |
console.log("CHECK", i, business_days[i], today, rightnow, check) | |
} | |
result = matches.some(element => element) | |
console.log("OUTPUT ", result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment