Difference between two times in format (hh: mm: ss)

后端 未结 4 705
情书的邮戳
情书的邮戳 2021-01-01 04:08

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. Li

4条回答
  •  囚心锁ツ
    2021-01-01 04:41

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

提交回复
热议问题