7

I have two times, let say:

t1= '05:34:01' ;
t2= '20:44:44' ;

I want two evaluate difference between these two times in same format. Like the result of above must come as

t3= t2 - t1;  // 15:10:43 

What is the way to do it?

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Tirthankar Kundu
  • 695
  • 3
  • 9
  • 19

4 Answers4

13

Highly recommend you include moment.js in your project if you need to handle time.

Example:

var t1 = moment('05:34:01', "hh:mm:ss");
var t2 = moment('20:44:44', "hh:mm:ss");
var t3 = moment(t2.diff(t1)).format("hh:mm:ss");

Working jsFiddle

To install moment.js in Node.js, simply do:

npm install moment (or for a global install sudo npm -g install moment)

And then in your Node.js, include it like so:

var moment = require('moment');

Edit: For 24h clock, change hh to HH.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
  • oh..now I noticed, the difference that I am getting is in 12 hr format. I actually needs 24 hr format like I mentioned in my question. I want answer to be like 15:10:43, but it is returning 03:10:43. Can you suggest how to get desired output. – Tirthankar Kundu Dec 16 '13 at 10:23
  • simply use `HH` instead of `hh` – Matt Johnson-Pint Dec 16 '13 at 16:04
  • @brandonscript, I am getting `12:10:43` value when I am running the fiddle (JFYI, I am in Tokyo). Is it something related to UTC? If yes how we can resolve it? Please suggest. – Vikasdeep Singh Dec 15 '17 at 06:39
  • Probably. Try setting the timezone for both moment objects? Not sure why they wouldn’t default to the same TZ though. – brandonscript Dec 15 '17 at 13:59
5

I would also go with the moment.js but you could do:

function time_diff(t1, t2) {
   var parts = t1.split(':');
   var d1 = new Date(0, 0, 0, parts[0], parts[1], parts[2]);
   parts = t2.split(':');
   var d2 = new Date(new Date(0, 0, 0, parts[0], parts[1], parts[2]) - d1);
   // this would also work
   // d2.toTimeString().substr(0, d2.toTimeString().indexOf(' '));
   return (d2.getHours() + ':' + d2.getMinutes() + ':' + d2.getSeconds());
}
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
3

I also used momentjs but with duration and got correct different between times:

var t1 = moment('05:34:01', "HH:mm:ss");
var t2 = moment('20:44:44', "HH:mm:ss");

var start_date = moment(t1, 'YYYY-MM-DD HH:mm:ss');
var end_date = moment(t2, 'YYYY-MM-DD HH:mm:ss');
var duration = moment.duration(end_date.diff(t1));

var t3 = duration.hours() + ":" + duration.minutes() + ":" + duration.seconds();
alert(t3);
console.log(duration.hours());
console.log(duration.minutes());
console.log(duration.seconds());
Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
2

try this

function time_diff(t1, t2) 
{
  var t1parts = t1.split(':');    
  var t1cm=Number(t1parts[0])*60+Number(t1parts[1]);

  var t2parts = t2.split(':');    
  var t2cm=Number(t2parts[0])*60+Number(t2parts[1]);

  var hour =Math.floor((t1cm-t2cm)/60);    
  var min=Math.floor((t1cm-t2cm)%60);    
  return (hour+':'+min+':00'); 
}

time_diff("02:23:00","00:45:00")
Vaghani Janak
  • 601
  • 5
  • 14