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