简体   繁体   中英

Difference between two timestamps in hours minutes and seconds

I am trying to calculate the difference between two timestamp "2020-03-18T17:34:45.856Z", "2020-03-18T16:34:45.856Z" the difference should be like this: 2 hours 20min 30sec

I have tried using

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

m not sure how to get the desired format

You need to get it manually using 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM