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

185
Visualizações
find next closest date and time from json response

I am getting a response from my api where start_date is given. I want to extract the id from json whose next date time is closest to current date time.

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

As you can see based on date id:2 and id:4 has the closest start_date from current date, and then based on time id:4 is the closest.

I am not able to figure out how to extract this id. I have been trying for hours but not getting anywhere close to the solution. Please help.

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

0

Step 1: Using Array.sort by Date.getTime() value when compare to current datetime.

Step 2: Return first item of sorted array.

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

const today = new Date().getTime();
const result = inputArray.sort((a, b) => {
   const diffA = new Date(a.expected.start_time).getTime() - today;
   const diffB = new Date(b.expected.start_time).getTime() - today;
   return diffA - diffB;
})[0];
console.log(result.expected.id);

about 4 years ago · Juan Pablo Isaza Relatório

0

My Data

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

Then, I have created a function getNextTime()

getNextTime = () => {
    let nextTimeData = [];
    const sortedData = data.sort((a, b) => new Date(b.expected.start_time) - new Date(a.expected.start_time))
    sortedData.forEach((element) => {
        if (new Date() <= new Date(element.expected.start_time)) {
            nextTimeData.push(element)
        }
    })
    return nextTimeData
}

That function gives you all the next date-time, you can use last index of nextTimeData as closet date-time like :

let data = this.getNextTime();
console.log('closet date time id ::', data[data.length - 1].expected.id);
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