0

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"
  • It looks like this part is wrong `currentHour+currentMinute*60` - should that be `(currentHour*60)+currentMinute` to convert to minutes? – freedomn-m Oct 06 '18 at 15:09

1 Answers1

1

Let javascript help you - parse the times into date objects, if 'to' is before 'from' then add a day, then just use <= and >=

function parseTime(t) {
  var d = new Date();
  var time = t.match(/(\d+)(?::(\d\d))?\s*(p?)/);
  d.setHours(parseInt(time[1]) + (time[3] ? 12 : 0));
  d.setMinutes(parseInt(time[2]) || 0);
  return d;
}

function isInRange(t, range) {
  var tt = parseTime(t);
  var timeFrom = parseTime(range.split("-")[0])
  var timeTo = parseTime(range.split("-")[1])
  if (timeTo < timeFrom) timeTo.setDate(timeTo.getDate() + 1);

  return tt >= timeFrom && tt <= timeTo;
}

console.log(isInRange("11:00", "10:30 - 14:30"))
console.log(isInRange("18:00", "10:30 - 14:30"))
console.log(isInRange("22:15", "20:00 - 08:00"))

parseTime taken from What is the best way to parse a time into a Date object from user input in Javascript?


To address your specific question regarding how to handle to<from, above I've used a Date object and added 1 day. In your code, you would check if to<from then add 24 hours to interval.endHour

freedomn-m
  • 27,664
  • 8
  • 35
  • 57