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

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

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 Denunciar

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 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