I'm trying to check if a time is within a time interval.
I have a currentTime-Variable (as string) that has exactly this format:
let currentTime = "08:45"
Also I have a timeInterval-Variable (as string) that has this format:
let timeInterval = "08:40 - 09:20"
What I'd like to detect is the time from the specified variable "currentTime
" fits into the given "time interval
"?
What I've tried so far is something like this:
let isInsideInterval = function(currentTime, timeInterval) {
let currentHour = parseInt(currentTime.split(":")[0]),
currentMinute = parseInt(currentTime.split(":")[1]),
interval = {
startHour: parseInt(timeInterval.split(" - ")[0].split(":")[0]),
startMinute: parseInt(timeInterval.split(" - ")[0].split(":")[1]),
endHour: parseInt(timeInterval.split(" - ")[1].split(":")[0]),
endMinute: parseInt(timeInterval.split(" - ")[1].split(":")[1])
}
let isAfterStart = (currentHour*60+currentMinute) - (interval.startHour*60+interval.startMinute) > 0 ? 1 : 0;
let isAfterEnd = (currentHour*60+currentMinute) - (interval.endHour*60+interval.endMinute) < 0 ? 1 : 0;
return isAfterStart && !isAfterEnd;
}
let currentTime_1 = "08:45"
let timeInterval_1 = "08:40 - 09:20"
let result_1 = isInsideInterval(currentTime_1, timeInterval_1)
console.log(`* '${currentTime_1}' is ${result_1 ? "inside" : "not inside"} timeinterval '${timeInterval_1}'`)
/* ----- */
let currentTime_2 = "5:02"
let timeInterval_2 = "22:40 - 09:20"
let result_2 = isInsideInterval(currentTime_2, timeInterval_2)
console.log(`* '${currentTime_2}' is ${result_2 ? "inside" : "not inside"} timeinterval '${timeInterval_2}'`)
The basics of the function are working but it looks like I'm missing some kind of "modulo"-operation - I do not know how to implement to also validate the timeInterval given these parameters:
let currentTime = "5:02"
let timeInterval = "22:40 - 09:20"