skyline33 - 7 months ago 21

Javascript Question

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

for example the one I have used is : **0:31.110 + 0:50.490 + 0:32.797**

which = **1:54.397**

so how to do this in javascript?

Here is 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 begin but I have just come up with the above code..

I need the output to be

Answer

You may use moment.js for this. That would make it very easy, as you can just parse the times in the correct format, add the `moments`

together ...

```
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"));
```

... and voila