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

133
Visualizações
find closest future date and time from today's from in json response

I am getting a response from my api where start_time is given. I want to extract the id from json whose next date time is closest to current date time which does not include dates from past.

An example of my json response:

[
    {
        "expected": {
            "id": 1,
            "start_time": "2021-10-01T06:35:00.659Z"
        }
    },
    {
        "expected": {
            "id": 2,
            "start_time": "2021-11-30T17:08:29.307Z"
        }
    },
    {   
        "expected": {
            "id": 3,
            "start_time": "2021-11-30T09:49:18.574Z"
        }
    },
    {   
        "expected": {
            "id": 4,
            "start_time": "2021-09-30T15:08:29.303Z"
        }
    },
]

Here what I have done untill now is to find the closest date time from today's date time:

const today = new Date().getTime();
const result = response.sort((a: any, b: any) => {
    const diffA =
        new Date(a.expected.shift_start_time).getTime() - today;
    const diffB =
        new Date(b.expected.shift_start_time).getTime() - today;
    return diffA - diffB;
})[0];

using this I am getting the id of closest date time from today. But the result sometimes also include a start_time from past date if that is closest to todays date.

I want to get id which does not include start_time from past dates. I am unable to get the logic how it can be done

Another approach which I did:

var today = moment();
for (let index = 0; index < response.length; index++) {
    const element = response[index].start_time;
    var dateDiff = today.diff(element, 'days') * -1;
    if (dateDiff >= 0) {
        console.log('>>d>>', dateDiff);
    }
}

console:

>>d>> -0
>>d>> 1
>>d>> -0

From here how can I return the required response?? please help

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe don't sort the list at all. Since you only need one dateTime which is closest with current Date, sorting the entire 'list' doesn't appear to be the optimal way to have your answer.You can use the following approach,

  • Save current DateTimeStamp to a local var
  • Start a loop through your list
  • Perform a check for your current item if the date is greater than local timeStamp you stored.
  • For first element, store the element as your current answer and for every other element compare the diff with the saved timeStamp and modify your answer accordingly.
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