Javascript - Loop into an array with a date time data and check if the dates are between the dates inside the array

Viewed 40

I am using moment.js, i have this kind of data in my Array/Object.

[{"start_datetime":"05/18/2021 1:30pm","end_datetime":"05/18/2021 2:00pm"}, 
{"start_datetime":"05/18/2021 2:00pm","end_datetime":"05/18/2021 3:30pm"},
{"start_datetime":"05/18/2021 1:30pm","end_datetime":"05/18/2021 3:30pm"}]

I wanted to create a function that will read through this array and check if there's a value that is between the values in the array.

Example: The first key which is {"start_datetime":"05/18/2021 1:30pm","end_datetime":"05/18/2021 2:00pm"} is between the last key which is {"start_datetime":"05/18/2021 1:30pm","end_datetime":"05/18/2021 3:30pm"}

I am aware that we can use moment's isBetween function, but I am thinking on the most efficient way of looping all the values in the array.

Thanks in advance!

2 Answers

const ranges = [{
    "start_datetime": "05/18/2021 1:30pm",
    "end_datetime": "05/18/2021 2:00pm"
  },
  {
    "start_datetime": "05/18/2021 2:00pm",
    "end_datetime": "05/18/2021 3:30pm"
  },
  {
    "start_datetime": "05/18/2021 1:30pm",
    "end_datetime": "05/18/2021 3:30pm"
  }
];

// for each of the ranges, compare to each other range
for (const d of ranges) {
  for (const r of ranges) {
    if (d === r) continue; // skip if the ranges are equal
    if (
      moment(d.start_datetime, "MM-DD-YYYY hh:mm:A") >= moment(r.start_datetime, "MM-DD-YYYY hh:mm:A") &&
      moment(d.end_datetime, "MM-DD-YYYY hh:mm:A") <= moment(r.end_datetime, "MM-DD-YYYY hh:mm:A")
    ) {
      console.log(d, "is within", r);
      continue;
    }
  }
}
<script src="https://momentjs.com/downloads/moment.js"></script>

I was able to answer it using this code:

const timeCollection = [{
    "start_datetime": "05/18/2021 1:30pm",
    "end_datetime": "05/18/2021 2:00pm"
  },
  {
    "start_datetime": "05/18/2021 2:00pm",
    "end_datetime": "05/18/2021 3:30pm"
  },
  {
    "start_datetime": "05/18/2021 1:30pm",
    "end_datetime": "05/18/2021 3:30pm"
  }
];

let overlapped = false;
    for (let i=0; i<timeCollection.length;i++) {
        for (let a=0; a<timeCollection.length;a++) {
            if (i == a) {
                continue;
            }
            //if the start and end date are both the same with the comparison data.
            if (
                moment(timeCollection[i].start_datetime, "MM-DD-YYYY hh:mm:A").format("MM-DD-YYYY hh:mm:A") === moment(timeCollection[a].start_datetime, "MM-DD-YYYY hh:mm:A").format("MM-DD-YYYY hh:mm:A") &&
                moment(timeCollection[i].end_datetime, "MM-DD-YYYY hh:mm:A").format("MM-DD-YYYY hh:mm:A") === moment(timeCollection[a].end_datetime, "MM-DD-YYYY hh:mm:A").format("MM-DD-YYYY hh:mm:A")
            ) {
                overlapped = true;
            } else if (
                //checker to check if date and time are in between..
                //making sure also that the time belongs to the crew
                moment(timeCollection[i].start_datetime, "MM-DD-YYYY hh:mm:A").isBetween(
                    moment(timeCollection[a].start_datetime, "MM-DD-YYYY hh:mm:A"),
                    moment(timeCollection[a].end_datetime, "MM-DD-YYYY hh:mm:A")) ||
                    moment(timeCollection[i].end_datetime, "MM-DD-YYYY hh:mm:A").isBetween(
                        moment(timeCollection[a].start_datetime, "MM-DD-YYYY hh:mm:A"),
                        moment(timeCollection[a].end_datetime, "MM-DD-YYYY hh:mm:A"))) {
                overlapped = true;
            }
        }
    }

console.log(overlapped);

PS: I'm not sure if this is efficient, but this one does the job for me. Thanks!

Related