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

152
Vistas
Getting the difference between 2 timestamps - javascript

I'm trying to get the difference between 2 timestamps.

I have a array with saved timestamps and when i loop over them i'm trying to get the difference between then and now but it logs as "sent:", "Hours - 0 - Minutes 0 - Seconds - 0" and i was wondering if anyone knew why?

let messages = [{
  username: "test one",
  message: "testing one",
  time: new Date()
}, {
  username: "test two",
  message: "testing two",
  time: new Date()
}];

const duration = (difference) => {
  let secondsInMiliseconds = 1000,
    minutesInMiliseconds = 60 * secondsInMiliseconds,
    hoursInMiliseconds = 60 * minutesInMiliseconds;

  let differenceInHours = difference / hoursInMiliseconds,
    differenceInMinutes = differenceInHours % 1 * 60,
    differenceInSeconds = differenceInMinutes % 1 * 60;

  return {
    "hours": Math.floor(differenceInHours),
    "minutes": Math.floor(differenceInMinutes),
    "seconds": Math.floor(differenceInSeconds)
  }
};

console.log("Wait One minute befor logging / loop");

setTimeout(() => {
  for (let i = 0; i < messages.length; i++) {
    let now = new Date();

    let time = duration(now - messages[i].time);
    let sent = `Hours - ${time.hours} - Minutes ${time.hours} - Seconds - ${time.hours}`;
    console.log("sent:", sent);
  }
}, 30000);

Sorry if my english is not good.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As the comments mentioned, there seems to be some confusion about the modulo operator priority and then there is the bug about time.hour being used three times. Here's a fixed, simplified and shortened version. Since the variables declared in duration are only used once, and since it's a simple function, I took the liberty to inline the calculation.

let messages = [{
  username: "test one",
  message: "testing one",
  time: new Date()
}, {
  username: "test two",
  message: "testing two",
  time: new Date()
}];

const duration = (difference) => {
  return {
    hours: Math.floor(difference / (1000 * 60 * 60)),
    minutes: Math.floor((difference / (1000 * 60)) % 60),
    seconds: Math.floor((difference / 1000) % 60)
  }
};

console.log("Wait One minute befor logging / loop");

setTimeout(() => {
  for (let i = 0; i < messages.length; i++) {
    let now = new Date();
    let time = duration(now - messages[i].time);
    let sent = `Hours - ${time.hours} - Minutes ${time.minutes} - Seconds - ${time.seconds}`;
    console.log("sent:", sent);
  }
}, 30000);
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