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

172
Visualizações
How to find time difference in seconds using moment.js

How can I find time difference in seconds between two different timezones. I want to use moment for this - My start time is something like- 2022-09-04T07:29:39Z[UTC] and end time will be the current time in my frontend.

My code =

 var now  = new Date().getTime();
 var then = "2022-03-04T07:29:39Z[UTC]";
 var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss")); //NAN
 var d = moment.duration(ms);
 var s = d.format("hh:mm:ss");
 console.log("Time Difference =",s);

I need help in this, currently I am getting ms as NAN how can I correct it!

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

0

d.format is not a function exposed by momentjs instead of that you should write like below

var now  = new Date().getTime();
var then = "2022-03-04T07:29:39Z[UTC]";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss")); 
var d = moment.duration(ms);
var s = moment(d).format('hh:mm:ss')
console.log("Time Difference =",s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

This type of calculation can easily be done with Vanilla JavaScript:

 const now  = new Date().getTime(),
  then = new Date("2022-03-04T07:29:39Z"),
  tsec= Math.round((now-then)/1000),
  sec=tsec%60, tmin=(tsec-sec)/60,
  min=tmin%60, th=(tmin-min)/60,
  h=th%60, d=(th-h)/24;
 
 console.log(`Time Difference = ${d}d ${h}h ${min}m ${sec}s`);

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