Add time string values

I have three times in the format Minute: Seconds: Miliseconds, which I need to add together and get the total time.

for example, the one I used: 0: 31.110 + 0: 50.490 + 0: 32.797 which = 1: 54.397

so how to do it in javascript?

Here is the JS code

var sp1 = $('#table tr td:nth-child(2)').text()
var sp2 = $('#table tr td:nth-child(3)').text()
var sp3 = $('#table tr td:nth-child(4)').text()
var1 = sp1 + sp2 + sp3
$('td:nth-child(5)').html(var1);

I don’t know where to start, but I just came up with the code. I need output 1: 54.397 in the last td, but I get this 0: 31.1100: 50.4900: 32.797 shown in this example http://jsfiddle.net/q1kumbea/

+4
source share
2 answers

moment.js . , , moments ...

var sp1 = $('#table tr td:nth-child(2)').text()
var sp2 = $('#table tr td:nth-child(3)').text()
var sp3 = $('#table tr td:nth-child(4)').text()
var1 = moment(sp1, "mm:ss.SSS") + moment(sp2, "mm:ss.SSS") + moment(sp3, "mm:ss.SSS")
 $('td:nth-child(5)').html(moment(var1).format("mm:ss.SSS"));

... voila

enter image description here

​​

+6

, (:)) , .

var plusTimes = function(arr) {
var resultTime =0;

for(var i = 0; i < arr.length; i ++) {
resultTime += (parseInt((arr[i].split(':')[0]) * 60  * 1000) + ( parseInt(arr[i].split(':')[1].split('.')[0]) * 1000 ) + parseInt(arr[i].split('.')[1])) 

}
var ms = (resultTime / 1000).toString().split('.')[1];
var sec = parseInt((resultTime / 1000).toString().split('.')[0]) % 60;
var min = (parseInt((114397 / 1000).toString().split('.')[0]) - parseInt((114397 / 1000).toString().split('.')[0]) % 60) / 60;

return min + ':' + sec + '.' + ms;

}
plusTimes(['0:31.110','0:50.490', '0:32.797']) // outputs "1:54.397"

, ,

+3

Source: https://habr.com/ru/post/1650955/


All Articles