here is a working example:
const times = [
{ from: "07:00", to: "10:00" },
{ from: "12:00", to: "15:00" },
{ from: "16:00", to: "20:00" },
{ from: "22:00", to: "02:00" }
];
function checkBetweenTimes(timesArr, timeToCheck) {
timeToCheck = timeToCheck.replace(":", "");
for (const time of timesArr) {
time.from = time.from.replace(":", "");
time.to = time.to.replace(":", "");
if (timeToCheck >= time.from && timeToCheck <= time.to) {
// make a array from time wich is currently "1300" into [1,3,0,0]
timeToCheck = timeToCheck.split("");
time.from = time.from.split("");
time.to = time.to.split("");
// add ":" at index 2 to make it a time again [1,3,:,0,0]
timeToCheck.splice(2, 0, ":");
time.from.splice(2, 0, ":");
time.to.splice(2, 0, ":");
// join the array back to "13:00"
timeToCheck = timeToCheck.join("");
time.from = time.from.join("");
time.to = time.to.join("");
return `${timeToCheck} is in between ${time.from} and ${time.to}`;
}
}
}
console.log(checkBetweenTimes(times, "13:00"));