Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
Next future date not picked from rota schedule correctly

I have a bit of an issue with this block of code.

It is meant to find the next rota schedule in an array, ignoring any previous ones.

However if the today's date is 2022-03-09, it picks up 2022-03-08T13:00:00.000Z as the next possible future date. I think this is wrong. It should return 2023-04-13T13:00:00.000Z.

How can I fix this?

export const rotaSchedules = [
  '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-08T13: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',
];

export const isWithin5Days = () => {
  const today = new Date();
  const futureDatesWithToday = rotaSchedules.map((date) => ({
    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,
    isWithin5Days: diff < 5 * 24 * 60 * 60 * 1000,
  };
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Before mapping rotaSchedules, you should first remove all the date that are in the past. To do so, you should write something like this

const futureDatesWithToday = rotaSchedules
    .filter((date) => new Date(date).getTime() - today.getTime() > 0)
    .map((date) => ({
        date,
        diff: Math.abs(new Date(date).getTime() - today.getTime()),
     }));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!