Skip to content
Advertisement

Calculate the difference between two dates and give the result in hh:mm format

date1=2023-01-02T12:22:00
date2=2023-01-03T22:15:00

How could I found the time difference?

Advertisement

Answer

function secondsToHMS(secs) {
  function z(n){return (n<10?'0':'') + n;}
  var sign = secs < 0 ? '-' : '';
  secs = Math.abs(secs);
  return sign + z(secs/3600 |0) + ':' + z((secs%3600) / 60 |0);
}

var d1 = new Date('2023-01-03T22:15:00');
var d2 = new Date('2023-01-02T12:22:00');

//console.log( secondsToHMS((d1 - d2) / 1000)); // 33:53
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement