Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if today is within 5 days of any of the future dates in array

If I have a list of arrays as follows, what would be the quickest way to check if today i.e. 2022-03-08T23:30:00.000Z is within 5 days of any of the future dates and return true along with the nearest future date (within a 5 day window)?

For example, in this example, it would return 2022-03-13T13:00:00.000Z as the nearest future date and return as true because it falls under this date.

export const nextRotaSchedules = [
  '2021-12-13T12:00:00.000Z',
  '2021-12-13T14:00:00.000Z',
  '2022-01-13T14:00:00.000Z',
  '2022-01-23T14:00:00.000Z',
  '2022-01-24T13:00:00.000Z',
  '2022-02-12T17:02:33.000Z',
  '2022-02-12T17:03:59.000Z',
  '2022-02-12T17:36:18.000Z',
  '2022-02-12T17:36:48.000Z',
  '2022-02-12T17:37:22.000Z',
  '2022-02-12T17:37:45.000Z',
  '2022-02-13T13:00:00.000Z',
  '2022-02-13T13:49:16.000Z',
  '2022-02-13T14:00:00.000Z',
  '2022-03-13T13:00:00.000Z',
  '2022-04-13T13:00:00.000Z',
  '2022-05-13T13:00:00.000Z',
  '2022-06-13T13:00:00.000Z',
  '2022-07-13T13:00:00.000Z',
  '2022-08-13T13:00:00.000Z',
  '2022-09-13T13:00:00.000Z',
  '2022-10-13T13:00:00.000Z',
  '2022-11-13T14:00:00.000Z',
  '2022-12-13T14:00:00.000Z',
  '2023-01-13T14:00:00.000Z',
  '2023-02-13T14:00:00.000Z',
  '2023-03-13T13:00:00.000Z',
  '2023-04-13T13:00:00.000Z',
  '2023-05-13T13:00:00.000Z',
  '2023-06-13T13:00:00.000Z',
  '2023-07-13T13:00:00.000Z',
  '2023-08-13T13:00:00.000Z',
  '2023-09-13T13:00:00.000Z',
  '2023-10-13T13:00:00.000Z',
  '2023-11-13T14:00:00.000Z',
  '2023-12-13T14:00:00.000Z',
  '2024-01-13T14:00:00.000Z',
];

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const nextRotaSchedules = [
  '2021-12-13T12:00:00.000Z',
  '2021-12-13T14:00:00.000Z',
  '2022-01-13T14:00:00.000Z',
  '2022-01-23T14:00:00.000Z',
  '2022-01-24T13:00:00.000Z',
  '2022-02-12T17:02:33.000Z',
  '2022-02-12T17:03:59.000Z',
  '2022-02-12T17:36:18.000Z',
  '2022-02-12T17:36:48.000Z',
  '2022-02-12T17:37:22.000Z',
  '2022-02-12T17:37:45.000Z',
  '2022-02-13T13:00:00.000Z',
  '2022-02-13T13:49:16.000Z',
  '2022-02-13T14:00:00.000Z',
  '2022-03-13T13:00:00.000Z',
  '2022-04-13T13:00:00.000Z',
  '2022-05-13T13:00:00.000Z',
  '2022-06-13T13:00:00.000Z',
  '2022-07-13T13:00:00.000Z',
  '2022-08-13T13:00:00.000Z',
  '2022-09-13T13:00:00.000Z',
  '2022-10-13T13:00:00.000Z',
  '2022-11-13T14:00:00.000Z',
  '2022-12-13T14:00:00.000Z',
  '2023-01-13T14:00:00.000Z',
  '2023-02-13T14:00:00.000Z',
  '2023-03-13T13:00:00.000Z',
  '2023-04-13T13:00:00.000Z',
  '2023-05-13T13:00:00.000Z',
  '2023-06-13T13:00:00.000Z',
  '2023-07-13T13:00:00.000Z',
  '2023-08-13T13:00:00.000Z',
  '2023-09-13T13:00:00.000Z',
  '2023-10-13T13:00:00.000Z',
  '2023-11-13T14:00:00.000Z',
  '2023-12-13T14:00:00.000Z',
  '2024-01-13T14:00:00.000Z',
];
const isWithin5Days = (today, futureDates) => {
  let isWithin5Days = false;
  let nearestFutureDate = '';
  for (let i = 0; i < futureDates.length; i++) {
    const futureDate = futureDates[i];
    const difference = new Date(futureDate) - today;
    if (difference > 0 && difference < 5 * 24 * 60 * 60 * 1000) {
      isWithin5Days = true;
      nearestFutureDate = futureDate;
      break;
    }
  }
  return { isWithin5Days, nearestFutureDate };
}
console.log(isWithin5Days(new Date(), nextRotaSchedules));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading