SoFunction
Updated on 2025-03-10

JavaScript implements to determine whether the time interval is one day in a row

In development, we often need to judge whether the time intervals of objects in an array are continuous. This article will introduce a method to determine whether the time is continuous from 00:00 to 24:00 by sorting and comparing the objects in the array.

background

Suppose we have an array with multiple objects, each with a start time and an end time. We need to judge whether these time intervals are continuous, that is, whether there is no interval from 00:00 to 24:00.

step

The following are the steps to determine whether the time interval is continuous:

First, we need to sort the objects in the array by the start time. This can be achieved by using JavaScript's sort method and custom comparison functions. The comparison function compares the start time of two objects and returns a negative, zero, or positive number to indicate their order.

((a, b) => ());

Next, we use a loop to iterate over the sorted array and start the comparison from the second object. We will compare the start time of the current object with the end time of the previous object.

for (let i = 1; i < ; i++) {
  const prevEndTime = intervals[i - 1].endTime;
  const currStartTime = intervals[i].startTime;
  // Determine whether there is any overlap in time  if (intervals[i].startTime < intervals[i - 1].endTime) {
    return 'Time coincides';
  }
  // Determine whether the time is continuous  if (prevEndTime !== currStartTime) {
    return 'Time is not continuous';
  }
}

Finally, we also need to check whether the start time of the first object is '00:00' and whether the end time of the last object is '24:00' to ensure that the time interval from 00:00 to 24:00 is continuous.

if (
  intervals[0].startTime !== '00:00' ||
  intervals[ - 1].endTime !== '24:00'
) {
  return 'Time is not continuous';
}

The complete function code is as follows:

export const checkTimeIntervalsValid = (
  intervals
) => {
  ((a, b) => ());

  for (let i = 1; i < ; i++) {
    const prevEndTime = intervals[i - 1].endTime;
    const currStartTime = intervals[i].startTime;

    if (intervals[i].startTime < intervals[i - 1].endTime) {
      return 'Time coincides';
    }

    if (prevEndTime !== currStartTime) {
      return 'Time is not continuous';
    }
  }

  if (
    intervals[0].startTime !== '00:00' ||
    intervals[ - 1].endTime !== '24:00'
  ) {
    return 'Time is not continuous';
  }

  return true;
};

Summarize

By sorting and comparing objects in the array, we can use the above steps to determine whether the time interval is continuous. First, we sort the objects by start time and then iterate over the array for comparison. If the time is found to have overlap or discontinuous, we will return the corresponding error message. Finally, we also check the start time of the first object and the end time of the last object to ensure that there is no interval between 00:00 and 24:00.

Using this method, we can easily judge whether the time intervals of objects in an array are continuous, providing convenience for our development work.

This is the article about JavaScript realization to determine whether the time interval is continuous for one day. For more relevant content on JavaScript judging time intervals, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!