Johnston
Johnston

Reputation: 20884

Add durations together in Javascript

Given an array of times:

["01:08", "03:46", "03:24", "05:53", "01:45", "03:32", "05:19", "08:56", "01:49", "05:40", "05:21", "02:40", "04:26", "02:02", "04:42", "03:58", "02:06", "02:46", "05:21", "03:37", "02:55", "03:26", "04:16", "01:32", "01:42", "03:22", "01:55", "01:41", "05:10", "00:45", "03:23", "05:08", "02:22", "02:34", "02:49", "01:18", "02:13", "01:37", "03:36", "05:26", "05:00", "02:41", "03:08", "01:00", "02:19", "02:33", "03:43", "01:35", "02:59", "01:38", "04:05", "04:15", "03:43", "03:43", "00:25"]

Where each time is a duration of minutes and seconds.

How can I add those durations together to get one final duration?

I tried using the date object but I can't seem to figure it out.

Upvotes: 3

Views: 1552

Answers (6)

void
void

Reputation: 36703

Whats up with this cute little piece of code:

function sumUp(x){
  var min = 0;
  [].forEach.call(x, function(inst){
    var _x = inst.split(":");
    min += +(_x[0])*60 + +(_x[1]);
  });
  return Math.floor(min/60)+":"+(min%60);
}

Here x is an array of time.

Upvotes: 0

Riad Baghbanli
Riad Baghbanli

Reputation: 3319

var da = ["01:08", "03:46", "03:24", "05:53", "01:45", "03:32", "05:19", "08:56", "01:49", "05:40", "05:21", "02:40", "04:26", "02:02", "04:42", "03:58", "02:06", "02:46", "05:21", "03:37", "02:55", "03:26", "04:16", "01:32", "01:42", "03:22", "01:55", "01:41", "05:10", "00:45", "03:23", "05:08", "02:22", "02:34", "02:49", "01:18", "02:13", "01:37", "03:36", "05:26", "05:00", "02:41", "03:08", "01:00", "02:19", "02:33", "03:43", "01:35", "02:59", "01:38", "04:05", "04:15", "03:43", "03:43", "00:25"];

var mins = 0;
var secs = 0;

for(var i=0; i<da.length; i++) {
  mins = mins + Number(da[i].substr(0, 2));
  secs = secs + Number(da[i].substr(3, 2));
}

mins = mins + Math.floor(secs / 60);
secs = secs % 60;

console.log(mins + ":" + secs);

Upvotes: 0

user663031
user663031

Reputation:

Split each entry into minutes and seconds and compute a decimal number of minutes. Add those up, then convert back into minutes and seconds.

Here's a little utility function to sum up an array:

function add(a, b) { return a + b; }
function sum(a)    { return a.reduce(add); }

Write another little function to split a time entry and return decimal number of minutes. We'll use this in map below:

function split(x) { 
  var parts = elt.split(':');
  return +parts[0] + parts[1]/60;
}

Use the sum function to sum the results of mapping each element of your input array into a decimal number of minutes, using split:

var minutes = sum(array.map(split));

Now you can print out the resulting number of minutes and seconds:

console.log(Math.floor(minutes), ":", (minutes - Math.floor(minutes))*60);

Upvotes: 0

Saad
Saad

Reputation: 53809

For a more functional style, you can use map to turn each time into seconds format, and then use reduce to get the sum of all the times.

function toSeconds(time) {
  var minutes = Number(time.slice(0, 2));
  var seconds = Number(time.slice(3));
  return seconds + minutes * 60;
}

function sum(a, b) {
  return a + b;
}

// Assuming your array is named 'arr'
var totalSeconds = arr.map(toSeconds).reduce(sum);
console.log(totalSeconds);

Upvotes: 3

Will
Will

Reputation: 66

Here's one more:

var t = ["01:08", "03:46", "03:24", "05:53", "01:45", "03:32", "05:19", "08:56", "01:49", "05:40", "05:21", "02:40", "04:26", "02:02", "04:42", "03:58", "02:06", "02:46", "05:21", "03:37", "02:55", "03:26", "04:16", "01:32", "01:42", "03:22", "01:55", "01:41", "05:10", "00:45", "03:23", "05:08", "02:22", "02:34", "02:49", "01:18", "02:13", "01:37", "03:36", "05:26", "05:00", "02:41", "03:08", "01:00", "02:19", "02:33", "03:43", "01:35", "02:59", "01:38", "04:05", "04:15", "03:43", "03:43", "00:25"];

var seconds = t.map(function(v) {
                      var ary = v.split(':');
                      return +ary[0] + ary[1] / 60;
              }).reduce(function(p, c) {
                          return p + c;
              });

Upvotes: 2

Gershom Maes
Gershom Maes

Reputation: 8150

Keep a running total of seconds (seconds + (minutes * 60)) as you loop through the array. In the end convert from seconds to MM:SS format.

var data = ["01:08", "03:46", "03:24", "05:53", "01:45", "03:32", "05:19", "08:56", "01:49", "05:40", "05:21", "02:40", "04:26", "02:02", "04:42", "03:58", "02:06", "02:46", "05:21", "03:37", "02:55", "03:26", "04:16", "01:32", "01:42", "03:22", "01:55", "01:41", "05:10", "00:45", "03:23", "05:08", "02:22", "02:34", "02:49", "01:18", "02:13", "01:37", "03:36", "05:26", "05:00", "02:41", "03:08", "01:00", "02:19", "02:33", "03:43", "01:35", "02:59", "01:38", "04:05", "04:15", "03:43", "03:43", "00:25"];

var durationSum = function(data) {
    var totalSeconds = 0;

    for (var i = 0; i < data.length; i++) {
        var split = data.split(':');
        var minutes = parseInt(split[0]);
        var seconds = parseInt(split[1]);

        totalSeconds = seconds + (minutes * 60);
    }

    var minutes = Math.floor(totalSeconds / 60);
    var seconds = totalSeconds - minutes;

    minutes = minutes.toString();
    seconds = seconds.toString();
    while (minutes.length < 2) minutes = '0' + minutes;
    while (seconds.length < 2) seconds = '0' + seconds;

    return minutes + ':' + seconds;
}

console.log(durationSum(data));

Upvotes: 1

Related Questions