Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Averaging Times using Javascript

I am building an app using Phonegap and JQuery.

The app stores ( using window.localStorage ) a set of times (no more than 10) in the format.

    HH:MM:SS.mm

There are a number of 'Zero' times in the list eg '00:00:00.00' which iphonegap, javascript eliminate using..

    function removeA(arr){
        var what, a= arguments, L= a.length, ax;
        while(L> 1 && arr.length){
            what= a[--L];
            while((ax= arr.indexOf(what))!= -1){
                arr.splice(ax, 1);
            }
        }
        return arr;
    }

    scores.sort();
    removeA(scores,'00:00:00.00');

so that i'm left with the fastest time first, and only the times that have a value.

I need to produce from the remaining values the average of those times.

eg:  00:00:03.00
     00:00:05.00
     00:00:02.00
     00:00:06.00

=    00:00:04.00

thanks in advance :)

like image 671
Deano Avatar asked Dec 01 '25 22:12

Deano


2 Answers

var times= [ '00:00:03.00', '00:00:05.00', '00:00:02.00', '00:00:06.00'],
    date = 0,
    result = '';
function offsetify(t){
    return t < 10 ? '0' + t : t;
}
for(var x = 0; x < times.length; x++ ) {
    var tarr = times[x].split(':');
    date += new Date(0, 0, 0, tarr[0], tarr[1], tarr[2].split('.')[0], tarr[2].split('.')[1]).getTime();   
}
var avg = new Date(date/times.length);
result = offsetify(avg.getHours()) + ':' + offsetify(avg.getMinutes()) + ':' + offsetify(avg.getSeconds()) + '.' + offsetify(avg.getMilliseconds());

DEMO

like image 179
thecodeparadox Avatar answered Dec 03 '25 14:12

thecodeparadox


if you are going to also have millisecond values and you want to consider them, then convert the times into millisecond. Now, add them and divide them by the number of records. Else, convert everything to seconds and find the average - you get the answer in seconds, of course.

The conversion is quite simple if take little time to think over it. Here's how to convert.

To milliseconds:

function convertToMS(timeStr) {      // timeStr in format 'HH:MM:SS.mm'
    var I = parseInt;  // for brevity
    var t = timeStr,
        h = I( t.substr(0,2) ),
        m = I( t.substr(3,2) ),
        s = I( t.substr(6,2) ),
        ms = I( t.substr(9,2) );

    return h * 3600000 + m * 60000 + s * 1000 + ms;
}

To seconds:

function convertToS(timeStr) {      // timeStr in format 'HH:MM:SS[.mm]'  -- .mm is ignored.
    var I = parseInt;  // for brevity
    var t = timeStr,
        h = I( t.substr(0,2) ),
        m = I( t.substr(3,2) ),
        s = I( t.substr(6,2) );

    return h * 3600 + m * 60 + s;
}

After the conversion's done, add them up and find the average.

UPDATE: To convert back to the format 'HH:MM:SS.mm', we change back the time into 'chunks' of hours, minutes, seconds and (if applicable) milliseconds.

function chunkifyFromSec(time) {  // time in s
    var t = "",
        h = Math.floor(time / 3600),
        m = Math.floor( (t - (h * 3600)) / 60 ),
        s = t - (h * 3600) - (m * 60);
    return {
        HH: h, MM: m, SS: s, mm: 0
    };
}
function chunkifyFromMS(time) {   // time in ms
    var t = "",
        h = Math.floor(time / 3600000),
        m = Math.floor( (t - (h * 3600000)) / 60000 ),
        s = Math.floor( (t - (h * 3600000) - (m * 60000)) / 1000 ),
        mm = t - (h * 3600000) - (m * 600000) - (s * 1000);
    return {
        HH: h, MM: m, SS: s, mm: mm
    };
}

Then, we return the string in the format 'HH:MM:SS.mm' using this:

function toTimeStr(chunks) {
    return
           (chunks.HH < 0 ? '0' : '') + chunks.HH + ":"
           += (chunks.MM < 0 ? '0' : '') + chunks.MM + ":"
           += (chunks.SS < 0 ? '0' : '') + chunks.SS + "."
           += (chunks.mm < 0 ? '0' : '') + chunks.mm
}
like image 36
Parth Thakkar Avatar answered Dec 03 '25 13:12

Parth Thakkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!