Home > Net >  How can I get the closest after date from the current date?
How can I get the closest after date from the current date?

Time:12-02

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

CodePudding user response:

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

CodePudding user response:

let sorted = bookingsList.sort((a,b) => Date.parse(a.start) - Date.parse(b.start));
let closestToRightNow = sorted.find(x => Date.parse(x.start) > Date.now()); // Date.now() is current time if you need something else use that

console.log(closestToRightNow.start);

CodePudding user response:

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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related