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

时间:2021-02-11 04:31:45

标签: javascript angularjs angular momentjs moment-timezone

我正在尝试计算两个时间戳之间的差异 “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');

我不知道如何获得所需的格式

1 个答案:

答案 0 :(得分:3)

您需要使用 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>