Home > front end >  Check if today is within 5 days of any of the future dates in array
Check if today is within 5 days of any of the future dates in array

Time:03-09

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',
];

CodePudding user response:

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));

Strongly typed alternate:

const isWithin5Days = (futureDates: string[]) => {
  const today = new Date();
  const futureDatesWithToday = futureDates.map((date) => {
    return {
      date,
      diff: Math.abs(new Date(date).getTime() - today.getTime()),
    };
  });
  const sortedFutureDatesWithToday = futureDatesWithToday.sort(
    (a, b) => a.diff - b.diff
  );
  const nearestFutureDate = sortedFutureDatesWithToday[0].date;
  const diff = Math.abs(new Date(nearestFutureDate).getTime() - today.getTime());
  return {[nearestFutureDate]: diff < 5 * 24 * 60 * 60 * 1000};
}
  • Related