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

后端 未结 4 721
情书的邮戳
情书的邮戳 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:21

    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.

提交回复
热议问题