0

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);
Gagandeep
  • 83
  • 2
  • 18
  • Does this answer your question? [How to sort an object array by date property?](https://stackoverflow.com/questions/10123953/how-to-sort-an-object-array-by-date-property) – Kinglish Dec 01 '21 at 18:30
  • I think my solution would be More Generic Answer in the approved answer but I don't understand that completey – Gagandeep Dec 01 '21 at 18:41
  • `return dateA > dateB ? 1 : -1` should be `return dateA - dateB` as the former doesn't deal well with cases where *dateA* and *dateB* are the same date, see [*How to sort an object array by date property?*](https://stackoverflow.com/questions/10123953/how-to-sort-an-object-array-by-date-property) And given that the dates are ISO 8601 format, there's no need to cast the strings to Dates, they'll sort just fine lexically. – RobG Dec 02 '21 at 03:34

2 Answers2

1

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();
}));
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)
Kinglish
  • 23,358
  • 3
  • 22
  • 43