Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda