Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda