以小时、分钟和秒为单位的两个时间戳之间的差异

Difference between two timestamps in hours minutes and seconds

我正在尝试计算两个时间戳之间的差异 “2020-03-18T17:34:45.856Z”、“2020-03-18T16:34:45.856Z” 差异应该是这样的:2 小时 20 分钟 30 秒

我试过使用

return moment.utc(moment(startDate, 'HH:mm:ss').diff(moment(endDate, 'HH:mm:ss'))).format('HH:mm:ss');

我不确定如何获得所需的格式

您需要使用Moment Duration

手动获取

const startDate = "2020-03-18T17:34:45.856Z";
const endDate = "2020-03-18T16:34:45.856Z";

const diff = moment(startDate).diff(moment(endDate));

const duration = moment.duration(diff);
const hrs = duration.hours();
const mins = duration.minutes();
const secs = duration.seconds();

console.log(hrs + "hours " + mins + "min " + secs + "sec");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>