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

216
Visualizações
How to get the most recent date and time using momentjs

My response object containing startTime, I want to compare all the startTimes and get the latest time to display on timeline. How can I find the latest Date and time using moment.js Response I am getting -

[{
  name:"abc",
  type:"CALL",
  startTime:"2021-10-13T07:16:55Z",
},{
  name:"def",
  type:"CALL",
  startTime:"2021-10-13T07:18:57Z",
},{
  name:"ghi",
  type:"CALL",
  startTime:"2021-10-15T07:17:05Z",
},{
  name:"jkl",
  type:"CALL",
  startTime:"2021-11-03T12:07:52Z",
}]

My code -

response.forEach((d)=>{
   if(d.type == 'CALL'){
   console.log("latest start time ",d);
   $scope.view(d); //need to  pass the obj of latest startTime only
   $scope.$apply();
}else{
//Something else
}

})

How can I do this using JavaScript or moment?

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

0

You can sort array:

const dates = [{
  name:"abc",
  type:"CALL",
  startTime:"2021-10-13T07:16:55Z",
},{
  name:"def",
  type:"CALL",
  startTime:"2021-10-13T07:18:57Z",
},{
  name:"ghi",
  type:"CALL",
  startTime:"2021-10-15T07:17:05Z",
},{
  name:"jkl",
  type:"CALL",
  startTime:"2021-11-03T12:07:52Z",
}];

const sortedDates = dates.sort((a, b) => moment(a.startTime).diff(moment(b)))

And in sortedDates array at position sortedDates[0] you have latest date

about 4 years ago · Juan Pablo Isaza Relatório

0

Using moment().isBefore property can handle this,

const returnLatest =(res)=>{
  let latest = res[0];
  res.forEach(item=>{
    if(moment(item.startTime).isBefore(latest.startTime)){
      latest = item;
    }
  });
  return latest;
}

const data = [{
  name:"abc",
  type:"CALL",
  startTime:"2021-10-13T07:16:55Z",
},{
  name:"def",
  type:"CALL",
  startTime:"2021-10-13T07:18:57Z",
},{
  name:"ghi",
  type:"CALL",
  startTime:"2021-10-15T07:17:05Z",
},{
  name:"jkl",
  type:"CALL",
  startTime:"2021-11-03T12:07:52Z",
}]

console.log(returnLatest(data));

It gonna sort it out and return the object with the latest startTime

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