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

619
Vistas
Date difference in hours - Angular 2

How can I get difference in hours between two dates in angular2? I don't want to use external libs like moment.js.

Having, for example: incidentTime = '2017-03-05 11:26:16 AM' and creationTime = '2017-03-06 12:26:16 AM'

let time = +params.data.incidentTime - +params.data.creationTime;  
console.log("time: " + time);

It should return 25 hours, but It returns NaN.

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This should do the job:

const date1 = params.data.incidentTime;
const date2 = params.data.creationTime;

const diffInMs = Date.parse(date2) - Date.parse(date1);
const diffInHours = diffInMs / 1000 / 60 / 60;

console.log(diffInHours);

Use Math.floor to round the result down, or Math.ceil to round it up

about 4 years ago · Santiago Trujillo Denunciar

0

parse the date type parameters to javascript's Date type.

let date1 = new Date(params.data.incidentTime).getTime();
let date2 = new Date(params.data.creationTime).getTime();
let time = date1 - date2;  //msec
let hoursDiff = time / (3600 * 1000);
about 4 years ago · Santiago Trujillo Denunciar

0

Try this:-

let time:any = new Date("2017-03-05 11:26:16").getHours();
let date2:any = new Date("2017-03-06 12:26:16").getHours();
console.log(time -date2, time, date2, "sdfsd");

but It returns NaN

this is because you are using + sign before date which is converting youtr date into number format so returning NAN(not a number)

about 4 years ago · Santiago Trujillo 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