1

I'm working on an Angular project where I need to verify if the time intervals of different courses overlap. I'm using the areIntervalsOverlapping function from the date-fns library. However, it seems that the function is returning true for cases where there shouldn't be an overlap.

Here is the code I'm using:

import { areIntervalsOverlapping } from 'date-fns';

const selectedCourse = {
  id: 1,
  name: 'Chemical Engineering',
  week: 1,
  capacity: 12,
  active: 1,
  start_time_1: 1719241200,
  end_time_1: 1719248400,
  start_time_2: 1719586800,
  end_time_2: 1719594000
};

const courses = [
  { id: 1, name: 'Chemical Engineering', week: 1, capacity: 12, active: 1, start_time_1: 1719241200, end_time_1: 1719248400, start_time_2: 1719586800, end_time_2: 1719594000 },
  { id: 2, name: 'Mechanical Engineering', week: 1, capacity: 25, active: 1, start_time_1: 1719248400, end_time_1: 1719255600, start_time_2: 1719594000, end_time_2: 1719601200 },
  { id: 3, name: 'Industrial Engineering', week: 1, capacity: 25, active: 1, start_time_1: 1719241200, end_time_1: 1719259200, start_time_2: 1719586800, end_time_2: 1719604800 },
  { id: 4, name: 'Mechatronics Engineering Group 1', week: 1, capacity: 24, active: 1, start_time_1: 1719241200, end_time_1: 1719248400, start_time_2: 1719586800, end_time_2: 1719594000 },
  { id: 5, name: 'Mechatronics Engineering Group 2', week: 1, capacity: 24, active: 1, start_time_1: 1719252000, end_time_1: 1719259200, start_time_2: 1719597600, end_time_2: 1719604800 },
  { id: 6, name: 'Fashion Design', week: 1, capacity: 30, active: 1, start_time_1: 1719241200, end_time_1: 1719255600, start_time_2: 1719500400, end_time_2: 1719514800 },
  { id: 7, name: 'Financial Engineering', week: 1, capacity: 30, active: 1, start_time_1: 1719241200, end_time_1: 1719255600, start_time_2: 1719500400, end_time_2: 1719514800 },
  { id: 8, name: 'Communication, Art and Culture Rally (Morning Group)', week: 1, capacity: 60, active: 1, start_time_1: 1719244800, end_time_1: 1719259200, start_time_2: 1719590400, end_time_2: 1719604800 },
  { id: 9, name: 'Communication, Art and Culture Rally (Afternoon Group)', week: 1, capacity: 60, active: 1, start_time_1: 1719262800, end_time_1: 1719277200, start_time_2: 1719608400, end_time_2: 1719622800 },
  { id: 10, name: 'Education Sciences', week: 1, capacity: 25, active: 1, start_time_1: 1719331200, end_time_1: 1719342000, start_time_2: 1719504000, end_time_2: 1719514800 },
  { id: 11, name: 'Architecture', week: 1, capacity: 60, active: 1, start_time_1: 1719331200, end_time_1: 1719342000, start_time_2: 1719504000, end_time_2: 1719514800 },
  { id: 12, name: 'Philosophy and Social Sciences', week: 1, capacity: 25, active: 1, start_time_1: 1719500400, end_time_1: 1719509400, start_time_2: 1719586800, end_time_2: 1719595800 }
];

courses.forEach(c => {
  const selectedStart = new Date(selectedCourse.start_time_1 * 1000);
  const selectedEnd = new Date(selectedCourse.end_time_2 * 1000);
  const courseStart = new Date(c.start_time_1 * 1000);
  const courseEnd = new Date(c.end_time_2 * 1000);

  console.log(`Comparing intervals:\nCourse: ${c.name} (${c.id}) ${courseStart} - ${courseEnd}\nSelected: ${selectedCourse.name} (${selectedCourse.id}) ${selectedStart} - ${selectedEnd}`);

  const isOverlapping = areIntervalsOverlapping(
    { start: courseStart, end: courseEnd },
    { start: selectedStart, end: selectedEnd }
  );

  console.log(`Course ${c.name} (${c.id}) overlaps with selected: ${isOverlapping}`);
});

and all the results were (TRUE)

3
  • Please do not upload images of code/data/errors. Commented May 21, 2024 at 18:48
  • 1
    @ErnestoVazquez All the examples have data that overlaps, any date in the range matches another range it will return true, plz revalidate the scenarios! if Jun22 -Jun25 is overlapping Jun24-Jun28, because of Jun 24 and 25 within the range of the other range! Commented May 21, 2024 at 19:02
  • But in the example when it takes Mechanical Engineering it should return false, because the dates would be these Comparing intervals: course: Mechanical Engineering (2) Mon Jun 24 2024 11:00:00 GMT-0600 (Central Standard Time) - Fri Jun 28 2024 13:00:00 GMT-0600 (Central Standard Time) Selected: Chemical Engineering (1) Mon Jun 24 2024 09:00:00 GMT-0600 (Central Standard Time) - Fri Jun 28 2024 11:00:00 GMT-0600 (Central Standard Time) Commented May 21, 2024 at 19:13

1 Answer 1

1

I made a mistake selecting the intervals

import { areIntervalsOverlapping } from 'date-fns';

const selectedCourse = {
  id: 1,
  name: 'Chemical Engineering',
  week: 1,
  capacity: 12,
  active: 1,
  start_time_1: 1719241200,
  end_time_1: 1719248400,
  start_time_2: 1719586800,
  end_time_2: 1719594000
};

const courses = [
  { id: 1, name: 'Chemical Engineering', week: 1, capacity: 12, active: 1, start_time_1: 1719241200, end_time_1: 1719248400, start_time_2: 1719586800, end_time_2: 1719594000 },
  { id: 2, name: 'Mechanical Engineering', week: 1, capacity: 25, active: 1, start_time_1: 1719248400, end_time_1: 1719255600, start_time_2: 1719594000, end_time_2: 1719601200 },
  { id: 3, name: 'Industrial Engineering', week: 1, capacity: 25, active: 1, start_time_1: 1719241200, end_time_1: 1719259200, start_time_2: 1719586800, end_time_2: 1719604800 },
  { id: 4, name: 'Mechatronics Engineering Group 1', week: 1, capacity: 24, active: 1, start_time_1: 1719241200, end_time_1: 1719248400, start_time_2: 1719586800, end_time_2: 1719594000 },
  { id: 5, name: 'Mechatronics Engineering Group 2', week: 1, capacity: 24, active: 1, start_time_1: 1719252000, end_time_1: 1719259200, start_time_2: 1719597600, end_time_2: 1719604800 },
  { id: 6, name: 'Fashion Design', week: 1, capacity: 30, active: 1, start_time_1: 1719241200, end_time_1: 1719255600, start_time_2: 1719500400, end_time_2: 1719514800 },
  { id: 7, name: 'Financial Engineering', week: 1, capacity: 30, active: 1, start_time_1: 1719241200, end_time_1: 1719255600, start_time_2: 1719500400, end_time_2: 1719514800 },
  { id: 8, name: 'Communication, Art and Culture Rally (Morning Group)', week: 1, capacity: 60, active: 1, start_time_1: 1719244800, end_time_1: 1719259200, start_time_2: 1719590400, end_time_2: 1719604800 },
  { id: 9, name: 'Communication, Art and Culture Rally (Afternoon Group)', week: 1, capacity: 60, active: 1, start_time_1: 1719262800, end_time_1: 1719277200, start_time_2: 1719608400, end_time_2: 1719622800 },
  { id: 10, name: 'Education Sciences', week: 1, capacity: 25, active: 1, start_time_1: 1719331200, end_time_1: 1719342000, start_time_2: 1719504000, end_time_2: 1719514800 },
  { id: 11, name: 'Architecture', week: 1, capacity: 60, active: 1, start_time_1: 1719331200, end_time_1: 1719342000, start_time_2: 1719504000, end_time_2: 1719514800 },
  { id: 12, name: 'Philosophy and Social Sciences', week: 1, capacity: 25, active: 1, start_time_1: 1719500400, end_time_1: 1719509400, start_time_2: 1719586800, end_time_2: 1719595800 }
];

const selectedIntervals = [
  { start: new Date(selectedCourse.start_time_1 * 1000), end: new Date(selectedCourse.end_time_1 * 1000) },
  { start: new Date(selectedCourse.start_time_2 * 1000), end: new Date(selectedCourse.end_time_2 * 1000) }
];

courses.forEach(c => {
  const courseIntervals = [
    { start: new Date(c.start_time_1 * 1000), end: new Date(c.end_time_1 * 1000) },
    { start: new Date(c.start_time_2 * 1000), end: new Date(c.end_time_2 * 1000) }
  ];

  courseIntervals.forEach(courseInterval => {
    selectedIntervals.forEach(selectedInterval => {
      console.log(`Comparing intervals:\nCourse: ${c.name} (${c.id}) ${courseInterval.start} - ${courseInterval.end}\nSelected: ${selectedCourse.name} (${selectedCourse.id}) ${selectedInterval.start} - ${selectedInterval.end}`);

      const isOverlapping = areIntervalsOverlapping(courseInterval, selectedInterval);

      console.log(`Course ${c.name} (${c.id}) overlaps with selected: ${isOverlapping}`);
    });
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.