Skip to content

Instantly share code, notes, and snippets.

@israelalagbe
Created May 19, 2022 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save israelalagbe/90a4cd7c8fba33bf0be469225c250a53 to your computer and use it in GitHub Desktop.
Save israelalagbe/90a4cd7c8fba33bf0be469225c250a53 to your computer and use it in GitHub Desktop.
Check if current time overlap two time (time range)
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