Count duration between two times after midnight

前端 未结 3 1922
逝去的感伤
逝去的感伤 2021-01-14 12:58

How to count duration between two times?

var start = moment(\'17:00\', \"HH:mm\");
var end = moment(\'02:15\', \"HH:mm\");
moment.duration(end.diff(start)).a         


        
3条回答
  •  青春惊慌失措
    2021-01-14 13:39

    I think the problem is that you have 2 moment objects, one representing today 17:00 and one today 02:15. Though you want today 17:00 and tomorrow 02:15. Just use moment('02:15', "HH:mm").add(1, 'days') to get the next day.

    var start = moment('17:00', "HH:mm");
    var end = moment('02:15', "HH:mm").add(1, "days");
    var result = moment.duration(end.diff(start)).asHours();
    document.write(result);

提交回复
热议问题