1

im struggelting to get the right output from 2 dates.

var destinationTime = moment("2019-05-07T16:59:00");

moment.utc(moment(destinationTime, "DD/MM/YYYY HH:mm:ss").diff(moment(moment(), "DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss")

And im want to get the output only in HOURS, so its only works for a dates at the same day.

if the destinationTime is in for example 7 May, its ignore the day getting worng output : "03:11:22"

expected behavior return me the time between two dates by hours "216:11:22" <-- hours and minutes and seconds

Roberto
  • 187
  • 3
  • 15
  • 1
    A possible duplicate of your question. Please see https://stackoverflow.com/questions/25150570/get-hours-difference-between-two-dates-in-moment-js – Shahbazsaeed38 Apr 29 '19 at 10:54
  • Possible duplicate of [Get hours difference between two dates in Moment Js](https://stackoverflow.com/questions/25150570/get-hours-difference-between-two-dates-in-moment-js) – Vencovsky Apr 29 '19 at 11:39

2 Answers2

0

You could try

jsfiddle http://jsfiddle.net/thanseeh/ahq52xfj/1/

 var destinationTime = moment("2019-05-07T16:59:00");

moment.utc(moment(destinationTime, "yyyy-MM-dd'T'hh:mm:ss").diff(moment(new Date(), "yyyy-MM-dd'T'hh:mm:ss"))).format("HH:mm:ss")
Abdulla Thanseeh
  • 9,438
  • 3
  • 11
  • 17
  • you could change days to required props – Abdulla Thanseeh Apr 29 '19 at 10:57
  • 1
    it only show me the right hours 194, but i want the hours as Format with minutes and seconds "HH:mm:ss" – Roberto Apr 29 '19 at 11:08
  • its not work as expected, it IGNORE the actually date.the output is "02:27:22" 2 hours 27 min, when is should be somthing like 216 hours 216:27:22, because the destination date is 7/5/2019 – Roberto Apr 29 '19 at 11:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/192563/discussion-between-roberto-and-abdulla-thanseeh). – Roberto Apr 29 '19 at 11:36
0

Have you tried something like this?

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

Or depending on your needs a shorter version

var hours = Math.abs(date1 - date2) / 36e5;
  • 36e5 = 60*60*1000