简体   繁体   中英

get the time difference between two times in format hh:mm:ss

I have two times in HH:mm:ss format. I am trying to calculate the time difference between two times. like

 var timeStart = '01:00:24';
   var timeEnd = '01:00:34'
  var timeDiff = timeEnd - timeStart; 

How can i perform this with a javascript

you can prepone to both a fixed date and do the subtraction:

var start = '01:00:24';
var end = '01:00:34';
start = '2017-11-8' + start;
end = '2017-11-8' + end;
var diff = +end - +start; // outputs 10000 (in ms)

You can split them with : and take difference. And again join them with :

 var start = '01:00:24'; var end = '01:00:34'; var diff = start.split(':').map((item,index) => end.split(':')[index] - item).join(':') console.log(diff) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM