Created
May 19, 2022 15:37
-
-
Save israelalagbe/90a4cd7c8fba33bf0be469225c250a53 to your computer and use it in GitHub Desktop.
Check if current time overlap two time (time range)
This file contains hidden or 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
import { parse, addDays } from "date-fns"; | |
const currentTimeIsBetween = (startTime, endTime) => { | |
const currentDate = new Date(); | |
const startTimeDate = parse(startTime, "hh:mm aa", currentDate); | |
const endTimeDate = parse(endTime, "hh:mm aa", currentDate); | |
const start = startTimeDate.getTime(); | |
let end = endTimeDate.getTime(); | |
const current = currentDate.getTime(); | |
if (start <= end) { | |
return current >= start && current <= end; | |
} | |
//When the startTime is greater than endTime, that means endTime is the next day | |
end = | |
addDays(endTimeDate, 1).getTime(); | |
return current >= start && current <= end; | |
}; | |
export default currentTimeIsBetween |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment