我正在尝试计算两个时间戳之间的差异 “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');
我不知道如何获得所需的格式
答案 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>