如何使用时刻找到两次的差异?

How to find difference of two times using moment?

这些是我的时间格式;

const startTime = "20:00";
const endTime = "05:00";
const startDate = "2021-01-20T00:00:00+05:30"
const days = "1"; // it can be 0 also

现在,我需要使用 moment 找出 startTime 和 endTime 之间的差异,并且作为“days = 1”,这意味着 endTime 在第二天结束:

所以预期的输出是,

9hrs 0mints on 2021-01-21

( As the days says 1, we need to count one day and show the endDate and if days=0 means same date as start date )

如何使用 moment 执行此操作?

正如我所试,

var dif = moment.duration(endTime.diff(startTime))

但它给出错误“endTime.diff 不是一个函数”。请帮忙。

当持续时间超过24小时时,您可以使用这种方式。

var startTime = moment("20:23", "HH:mm");
var endTime = moment("05:10", "HH:mm");
var days ="1";

// calculate the days in milliseconds
var duration = moment.duration(parseInt(days), 'days');
var day = duration.asMilliseconds();

//calculate the total milliseconds
var ms = day + moment(endTime,"HH:mm").diff(moment(startTime,"HH:mm"));
var d = moment.duration(ms);

//get the total duration
var s = Math.floor(d.asHours())+" hrs " + moment.utc(ms).format("mm") + " mins";

console.log(s);