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

111
Views
How can I get the closest after date from the current date?

I am trying to get the closest date which would be the next date from the current date but I don't know how to get it. I tried to sort the booking lists array but it gave me the previous date.

This is my array :

const bookingsList = [
  {
    sitterName: 'John',
    start: '2021-12-09',
    end: '2021-12-09',
    status: 'accepted',
  },
  {
    sitterName: 'John',
    start: '2021-12-06',
    end: '2021-12-06',
    status: 'accepted',
  },
  {
    sitterName: 'John',
    start: '2021-12-08',
    end: '2021-12-08',
    status: 'accepted',
  },
  {
    sitterName: 'Guru',
    start: '2021-11-30',
    end: '2021-11-30',
    status: 'accepted',
  },
];

const sortedBookings = bookingsList.sort(sortFunction);

function sortFunction(a: any, b: any) {
  const dateA = new Date(a.start).getTime();
  const dateB = new Date(b.start).getTime();
  return dateA > dateB ? 1 : -1;
}

console.log(sortedBookings[0].start);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you have your array sorted than you can use the function find() to get the first element that matches a condition. And the condition could be element.date > Date.now(). Take a look at the code:

var found = sortedBookings.find((function (element) {
    return new Date(element.start) > Date.now();
}));
about 4 years ago · Juan Pablo Isaza Report

0

I'll probably get into trouble for this.. but here's one method. Add todays date into the array, sort it and pick the next item in the array. Then remove the array addition. It's not the best solution, but will get you to where you want to go.

const bookingsList = [{
    sitterName: 'John',
    start: '2021-12-09',
    end: '2021-12-09',
    status: 'accepted',
  },
  {
    sitterName: 'John',
    start: '2021-12-06',
    end: '2021-12-06',
    status: 'accepted',
  },
  {
    sitterName: 'John',
    start: '2021-12-08',
    end: '2021-12-08',
    status: 'accepted',
  },
  {
    sitterName: 'Guru',
    start: '2021-11-30',
    end: '2021-11-30',
    status: 'accepted',
  },
];

let today = new Date()
today = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + ("0" + today.getDate()).slice(-2);
console.log('today', today);
// does it exist?
if (!bookingsList.find(a => a.start === today)) {
  bookingsList.push({
    sitterName: 'deleteme',
    start: today
  });
  }
  let sortedBookings = bookingsList.sort(sortFunction);

  function sortFunction(a, b) {
    const dateA = new Date(a.start).getTime();
    const dateB = new Date(b.start).getTime();
    return dateA > dateB ? 1 : -1;
  }
  let index = bookingsList.findIndex(a => a.start === today);
  let target = sortedBookings[(index + 1)]?.start;
  console.log('next date', target);

  // now remove the placeholder if there
  sortedBookings = sortedBookings.filter(a => a.sitterName !== 'deleteme');
  
  console.log('final array', sortedBookings)

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!