Skip to content Skip to sidebar Skip to footer

Find Time Average Of Hh:mm:ss

I have this fiddle that calculates average time with miliseconds. However, my DB stores data in format of hh:mm:ss. fiddle var times= [ '00:00:03.00', '00:00:05.00', '00:00:02.00',

Solution 1:

You could get the seconds, round the value and build a new string of the average time.

functiongetAverageTime(array) {
    var times = [3600, 60, 1],
        parts = array.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0)),
        avg = Math.round(parts.reduce((a, b) => a + b, 0) / parts.length);

    return times
        .map(t => [Math.floor(avg / t), avg %= t][0])
        .map(v => v.toString().padStart(2, 0))
        .join(':');
}

console.log(getAverageTime(['00:00:03', '00:00:05', '00:00:020', '00:00:07']));
console.log(getAverageTime(['00:00:03', '00:30:05', '00:30:020', '03:00:07']));

ES5

functiongetAverageTime(array) {
    var times = [3600, 60, 1],
        parts = array.map(function (s) {
            return s.split(':').reduce(function (s, v, i) {
                return s + times[i] * v;
            }, 0);
        }),
        avg = Math.round(parts.reduce(function (a, b) {
            return a + b;
        }, 0) / parts.length);

    return times
        .map(function (t) {
            var value = Math.floor(avg / t);
            avg %= t;
            return value;
        })
        .map(function (v) {
            return v.toString().padStart(2, 0);
         })
        .join(':');
}

console.log(getAverageTime(['00:00:03', '00:00:05', '00:00:020', '00:00:07']));
console.log(getAverageTime(['00:00:03', '00:30:05', '00:30:020', '03:00:07']));

Solution 2:

var seconds = avg.getMilliseconds() > 500 ? avg.getSeconds() + 1 : avg.getSeconds();

result = offsetify(avg.getHours()) + ':' + offsetify(avg.getMinutes()) + ':' + 
offsetify(seconds);

Basically just checking to see if ms is more than 50, if it is can manually add one to seconds, if not leave seconds as is. Not sure if you wanted seconds to be offset as well.

Solution 3:

Date can be used to convert them to milliseconds :

functionaverageTime(arr) {
    var sum = arr.reduce(function(a, b) { return a + +newDate('1970T' + b + 'Z'); }, 0);
    returnnewDate(sum / arr.length + 500).toJSON().slice(11, 19);
}

console.log( averageTime(['00:00:03.00', '00:00:05.00', '00:00:02.00', '00:00:06.00']) );
console.log( averageTime(['00:00:03', '00:00:05', '00:00:20', '00:00:07']) );

Solution 4:

First, convert all times into their UNIX timestamps using Date.getTime(), producing an array, then average the results. example: ]

let times = [
1551984787316,
1551984789662,
1551984790162,
1551984790730,
1551984791310
]


letaverage = ( values ) => {
  let sum = 0;
  for( let i = 0; i < values.length; i++ ){
    sum += parseInt( values[i], 10 ); // base 10
  }

  returnMath.floor( sum/values.length );
}

let avgTime = average( times );
let avgDate = newDate( avgTime );
console.log( avgDate );

Post a Comment for "Find Time Average Of Hh:mm:ss"