0

I'm trying to calculate the difference between times.

['08:30', '09:30']  // End - Start should = 60 min

Here is what I have tried,

let timeEnd = moment(event.end).format('HH:mm');
let timeStart = moment(event.start).format('HH:mm');

However, I keep getting NaN

console.log(Number(timeEnd) - Number(timeStart));

What am I doing wrong here?

JustCarty
  • 3,839
  • 5
  • 31
  • 51
Deano
  • 11,582
  • 18
  • 69
  • 119
  • why are you trying to convert a time string to a number? moment has functions for getting time differences. – Daniel A. White Dec 28 '17 at 16:39
  • @DanielA.White I didn't know that moment has time difference function : / – Deano Dec 28 '17 at 16:40
  • 1
    https://momentjs.com/docs/#/displaying/difference/ – Chris Riebschlager Dec 28 '17 at 16:41
  • 1
    If you didn't use moment, but a plain Date object, this approach would work, as demonstrated by the documentation of the Number() constructor. But since a date object created by moment.js isn't the same as a native Date object, the Number() constructor can't call the correct methods to transform the Date into miliseconds. Just stick to using the moment API, it basically has everything. – Shilly Dec 28 '17 at 16:44
  • 1
    https://jsfiddle.net/yv2fgc02/ – Hackerman Dec 28 '17 at 16:45

0 Answers0