简体   繁体   中英

How to sum time fields javascript HH:MM

I have an adobe pdf that calculates elapsed time, which is working. I however want to sum those times to give a total time in HH:MM format. enter image description here Anyone have any ideas on the javascript code that could do this? enter image description here

In case i understand you correctly and you have an array of eplapsedTime as text, You can run over the values and sum the hours in left part and minutes in right with split.

 const flightTimes = ['09:12','12:13','02:55','23:40','05:59']; let hours = 0, minutes = 0; flightTimes.forEach((time) => { const split = time.split(':'); hours += parseInt(split[0]); minutes += parseInt(split[1]); }); const formattedNumber = (num) => ("0" + num).slice(-2); hours += Math.floor(minutes/60); minutes = minutes % 60; console.log(formattedNumber(hours) + ':' + formattedNumber(minutes));

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