How to check if current time is between two given time stored in Array

Viewed 72

I am trying to check if current time lies between the two timing using "isBetween" function of moment, but it is not working. I need to find the shift where this time belong to. Any suggestion or any other method.

var Time = [{
    "shiftName": "Shift A",
    "shiftCode": "shiftA",
    "startTime": "07:00",
    "endTime": "15:00",
  },
  {
    "shiftName": "Shift B",
    "shiftCode": "shiftB",
    "startTime": "15:00",
    "endTime": "23:00",
  },
  {
    "shiftName": "Shift C",
    "shiftCode": "shiftC",
    "startTime": "23:00",
    "endTime": "07:00",
  }

]

Time.forEach(element => {
  var st = moment(element.startTime, 'HH:mm');
  var et = moment(element.endTime, 'HH:mm');

  if (moment().isBetween(st, et)) {
    console.log(element.shiftName)
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

2 Answers

you can compare the time just like '07:30' > '05:30'. when it is a daytime all is easy - the moment should be later than start time and earlier than end time. But night time (for example, 23:00 - 07:00) is a bit tricky because start time is higher than end time. So you have to check what kind of span it is.

function findShift(moment){
  for (let i = 0; i < Time.length; i++) {
    const start = Time[i].startTime;
    const end = Time[i].endTime;
    if (start > end) {
      if (moment <= start || moment >=end) return Time[i].shiftName
    } else {
      if (moment >= start && moment <= end) return Time[i].shiftName 
    }
  }
  return null
}

The following function gives you the name of the current shift, but you can also pass a moment.js object to get the shift of a specific time.

For the edge cases that are part of multiple shifts (like "7:00", "15:00", ...) my script returns the first found shift.

function getShiftName(time = moment()) {
    const isBetween = (a, b, c) => ((a <= b && b <= c) || a > c && (a <= b || b <= c));
    for (shift of Time) {
        if (isBetween(shift.startTime, time.format("HH:mm"), shift.endTime)) {
            return shift.shiftName;
        }
    }
    return "none";
}

// your sample data, minified for a shorter code-block
var Time = [{"shiftName": "Shift A", "shiftCode": "shiftA", "startTime": "07:00", "endTime": "15:00"},{ "shiftName": "Shift B", "shiftCode": "shiftB", "startTime": "15:00", "endTime": "23:00", }, { "shiftName": "Shift C", "shiftCode": "shiftC", "startTime": "23:00", "endTime": "07:00", }];

// demo
console.log("now", getShiftName()); // the current shift name
console.log(" 12", getShiftName(moment(12,"HH"))); // shift A
console.log(" 18", getShiftName(moment(18,"HH"))); // shift B
console.log(" 23", getShiftName(moment(23,"HH"))); // shift B
console.log(" 24", getShiftName(moment(24,"HH"))); // shift C
console.log("  4", getShiftName(moment( 4,"HH"))); // shift C
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

Related