Find time average of hh:mm:ss2019 Community Moderator ElectionFind sum of time in an array of time format hh:mm:ssCalculate relative time in C#How to get the current time in PythonWhat do 'real', 'user' and 'sys' mean in the output of time(1)?Convert a Unix timestamp to time in JavaScriptHow do I get time of a Python program's execution?How to calculate the time interval between two time stringsGet current time and date on AndroidFind object by id in an array of JavaScript objectsHow to calculate average call time in Microsoft Accesscalculating average for time in hh:mm format

What does "Four-F." mean?

What can I do if I am asked to learn different programming languages very frequently?

Recruiter wants very extensive technical details about all of my previous work

Geography in 3D perspective

How to generate binary array whose elements with values 1 are randomly drawn

Describing a chess game in a novel

Could Sinn Fein swing any Brexit vote in Parliament?

두음법칙 - When did North and South diverge in pronunciation of initial ㄹ?

What favor did Moody owe Dumbledore?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

How are passwords stolen from companies if they only store hashes?

Asserting that Atheism and Theism are both faith based positions

What are substitutions for coconut in curry?

Comment Box for Substitution Method of Integrals

PTIJ What is the inyan of the Konami code in Uncle Moishy's song?

Are dual Irish/British citizens bound by the 90/180 day rule when travelling in the EU after Brexit?

How to terminate ping <dest> &

PTIJ: Why do we blow Shofar on Rosh Hashana and use a Lulav on Sukkos?

Can other pieces capture a threatening piece and prevent a checkmate?

How can an organ that provides biological immortality be unable to regenerate?

Knife as defense against stray dogs

How is the partial sum of a geometric sequence calculated?

How does 取材で訪れた integrate into this sentence?

Maths symbols and unicode-math input inside siunitx commands



Find time average of hh:mm:ss



2019 Community Moderator ElectionFind sum of time in an array of time format hh:mm:ssCalculate relative time in C#How to get the current time in PythonWhat do 'real', 'user' and 'sys' mean in the output of time(1)?Convert a Unix timestamp to time in JavaScriptHow do I get time of a Python program's execution?How to calculate the time interval between two time stringsGet current time and date on AndroidFind object by id in an array of JavaScript objectsHow to calculate average call time in Microsoft Accesscalculating average for time in hh:mm format










3















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', '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());
document.write(result);


I need to make sure when seconds are averaged for example to 8.75 then average is returned as 00:00:9
using array like this:



var times= [ '00:00:03', '00:00:05', '00:00:020', '00:00:07']


can someone please help me modify this to properly round off hh:mm:ss format. thanks.










share|improve this question

















  • 1





    I think Nina's is a great answer, but want to ask -- when you say "my DB stores data in format of hh:mm:ss" do you mean that you have a table which you are not allowed to alter that uses a datetime or timestamp field? ...because your DB certainly could store average milliseconds as an int.

    – Stephen P
    Mar 7 at 19:59















3















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', '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());
document.write(result);


I need to make sure when seconds are averaged for example to 8.75 then average is returned as 00:00:9
using array like this:



var times= [ '00:00:03', '00:00:05', '00:00:020', '00:00:07']


can someone please help me modify this to properly round off hh:mm:ss format. thanks.










share|improve this question

















  • 1





    I think Nina's is a great answer, but want to ask -- when you say "my DB stores data in format of hh:mm:ss" do you mean that you have a table which you are not allowed to alter that uses a datetime or timestamp field? ...because your DB certainly could store average milliseconds as an int.

    – Stephen P
    Mar 7 at 19:59













3












3








3








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', '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());
document.write(result);


I need to make sure when seconds are averaged for example to 8.75 then average is returned as 00:00:9
using array like this:



var times= [ '00:00:03', '00:00:05', '00:00:020', '00:00:07']


can someone please help me modify this to properly round off hh:mm:ss format. thanks.










share|improve this question














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', '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());
document.write(result);


I need to make sure when seconds are averaged for example to 8.75 then average is returned as 00:00:9
using array like this:



var times= [ '00:00:03', '00:00:05', '00:00:020', '00:00:07']


can someone please help me modify this to properly round off hh:mm:ss format. thanks.







javascript time






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 17:30









codercoder

819




819







  • 1





    I think Nina's is a great answer, but want to ask -- when you say "my DB stores data in format of hh:mm:ss" do you mean that you have a table which you are not allowed to alter that uses a datetime or timestamp field? ...because your DB certainly could store average milliseconds as an int.

    – Stephen P
    Mar 7 at 19:59












  • 1





    I think Nina's is a great answer, but want to ask -- when you say "my DB stores data in format of hh:mm:ss" do you mean that you have a table which you are not allowed to alter that uses a datetime or timestamp field? ...because your DB certainly could store average milliseconds as an int.

    – Stephen P
    Mar 7 at 19:59







1




1





I think Nina's is a great answer, but want to ask -- when you say "my DB stores data in format of hh:mm:ss" do you mean that you have a table which you are not allowed to alter that uses a datetime or timestamp field? ...because your DB certainly could store average milliseconds as an int.

– Stephen P
Mar 7 at 19:59





I think Nina's is a great answer, but want to ask -- when you say "my DB stores data in format of hh:mm:ss" do you mean that you have a table which you are not allowed to alter that uses a datetime or timestamp field? ...because your DB certainly could store average milliseconds as an int.

– Stephen P
Mar 7 at 19:59












4 Answers
4






active

oldest

votes


















1














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






function getAverageTime(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






function getAverageTime(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']));








share|improve this answer

























  • interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

    – coder
    Mar 7 at 17:57











  • do you have an example?

    – Nina Scholz
    Mar 7 at 18:41











  • I am having trouble adding your code to my code.. it doesn't like =>

    – coder
    Mar 7 at 18:55











  • maybe the ES5 version is working for you.

    – Nina Scholz
    Mar 7 at 19:01



















0














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.






share|improve this answer

























  • thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

    – coder
    Mar 7 at 17:54






  • 2





    there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

    – Steven Stark
    Mar 7 at 18:44











  • actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

    – coder
    Mar 7 at 18:47







  • 1





    Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

    – coder
    Mar 7 at 18:50






  • 1





    nice one Steven edited my answer accordingly

    – Carlos Reyes
    Mar 7 at 19:25


















0














Date can be used to convert them to milliseconds :






function averageTime(arr) 
var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
return new Date(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']) );








share|improve this answer






























    0














    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
    ]


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


    return Math.floor( sum/values.length );


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








    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049727%2ffind-time-average-of-hhmmss%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














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






      function getAverageTime(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






      function getAverageTime(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']));








      share|improve this answer

























      • interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

        – coder
        Mar 7 at 17:57











      • do you have an example?

        – Nina Scholz
        Mar 7 at 18:41











      • I am having trouble adding your code to my code.. it doesn't like =>

        – coder
        Mar 7 at 18:55











      • maybe the ES5 version is working for you.

        – Nina Scholz
        Mar 7 at 19:01
















      1














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






      function getAverageTime(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






      function getAverageTime(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']));








      share|improve this answer

























      • interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

        – coder
        Mar 7 at 17:57











      • do you have an example?

        – Nina Scholz
        Mar 7 at 18:41











      • I am having trouble adding your code to my code.. it doesn't like =>

        – coder
        Mar 7 at 18:55











      • maybe the ES5 version is working for you.

        – Nina Scholz
        Mar 7 at 19:01














      1












      1








      1







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






      function getAverageTime(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






      function getAverageTime(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']));








      share|improve this answer















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






      function getAverageTime(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






      function getAverageTime(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']));








      function getAverageTime(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']));





      function getAverageTime(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']));





      function getAverageTime(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']));





      function getAverageTime(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']));






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 7 at 19:13

























      answered Mar 7 at 17:50









      Nina ScholzNina Scholz

      191k15104176




      191k15104176












      • interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

        – coder
        Mar 7 at 17:57











      • do you have an example?

        – Nina Scholz
        Mar 7 at 18:41











      • I am having trouble adding your code to my code.. it doesn't like =>

        – coder
        Mar 7 at 18:55











      • maybe the ES5 version is working for you.

        – Nina Scholz
        Mar 7 at 19:01


















      • interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

        – coder
        Mar 7 at 17:57











      • do you have an example?

        – Nina Scholz
        Mar 7 at 18:41











      • I am having trouble adding your code to my code.. it doesn't like =>

        – coder
        Mar 7 at 18:55











      • maybe the ES5 version is working for you.

        – Nina Scholz
        Mar 7 at 19:01

















      interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

      – coder
      Mar 7 at 17:57





      interesting solution. will this work properly once there are minutes more than 0 to make sure properly rounds to hours... etc

      – coder
      Mar 7 at 17:57













      do you have an example?

      – Nina Scholz
      Mar 7 at 18:41





      do you have an example?

      – Nina Scholz
      Mar 7 at 18:41













      I am having trouble adding your code to my code.. it doesn't like =>

      – coder
      Mar 7 at 18:55





      I am having trouble adding your code to my code.. it doesn't like =>

      – coder
      Mar 7 at 18:55













      maybe the ES5 version is working for you.

      – Nina Scholz
      Mar 7 at 19:01






      maybe the ES5 version is working for you.

      – Nina Scholz
      Mar 7 at 19:01














      0














      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.






      share|improve this answer

























      • thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

        – coder
        Mar 7 at 17:54






      • 2





        there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

        – Steven Stark
        Mar 7 at 18:44











      • actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

        – coder
        Mar 7 at 18:47







      • 1





        Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

        – coder
        Mar 7 at 18:50






      • 1





        nice one Steven edited my answer accordingly

        – Carlos Reyes
        Mar 7 at 19:25















      0














      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.






      share|improve this answer

























      • thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

        – coder
        Mar 7 at 17:54






      • 2





        there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

        – Steven Stark
        Mar 7 at 18:44











      • actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

        – coder
        Mar 7 at 18:47







      • 1





        Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

        – coder
        Mar 7 at 18:50






      • 1





        nice one Steven edited my answer accordingly

        – Carlos Reyes
        Mar 7 at 19:25













      0












      0








      0







      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.






      share|improve this answer















      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.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 7 at 19:24

























      answered Mar 7 at 17:43









      Carlos ReyesCarlos Reyes

      988




      988












      • thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

        – coder
        Mar 7 at 17:54






      • 2





        there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

        – Steven Stark
        Mar 7 at 18:44











      • actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

        – coder
        Mar 7 at 18:47







      • 1





        Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

        – coder
        Mar 7 at 18:50






      • 1





        nice one Steven edited my answer accordingly

        – Carlos Reyes
        Mar 7 at 19:25

















      • thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

        – coder
        Mar 7 at 17:54






      • 2





        there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

        – Steven Stark
        Mar 7 at 18:44











      • actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

        – coder
        Mar 7 at 18:47







      • 1





        Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

        – coder
        Mar 7 at 18:50






      • 1





        nice one Steven edited my answer accordingly

        – Carlos Reyes
        Mar 7 at 19:25
















      thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

      – coder
      Mar 7 at 17:54





      thanks it worked well with seconds. I wonder if it will work properly with minutes..or if i need something similar for minutes,, can you plz review this and see if it works properly fiddle

      – coder
      Mar 7 at 17:54




      2




      2





      there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

      – Steven Stark
      Mar 7 at 18:44





      there is 1000 ms in 1 s, so wouldn't you want to test if it's greater than 500?

      – Steven Stark
      Mar 7 at 18:44













      actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

      – coder
      Mar 7 at 18:47






      actually can you please revisit this... I tried to average these values: 00:01:20, 00:00:40, 00:01:00, 00:00:09 result was given to me 00:00:48 instead of 00:00:47

      – coder
      Mar 7 at 18:47





      1




      1





      Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

      – coder
      Mar 7 at 18:50





      Bingo Steven. thank you for that comment. You answered my question before I asked it. checking it >500 did the trick and i did get 00:00:47

      – coder
      Mar 7 at 18:50




      1




      1





      nice one Steven edited my answer accordingly

      – Carlos Reyes
      Mar 7 at 19:25





      nice one Steven edited my answer accordingly

      – Carlos Reyes
      Mar 7 at 19:25











      0














      Date can be used to convert them to milliseconds :






      function averageTime(arr) 
      var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
      return new Date(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']) );








      share|improve this answer



























        0














        Date can be used to convert them to milliseconds :






        function averageTime(arr) 
        var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
        return new Date(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']) );








        share|improve this answer

























          0












          0








          0







          Date can be used to convert them to milliseconds :






          function averageTime(arr) 
          var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
          return new Date(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']) );








          share|improve this answer













          Date can be used to convert them to milliseconds :






          function averageTime(arr) 
          var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
          return new Date(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']) );








          function averageTime(arr) 
          var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
          return new Date(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']) );





          function averageTime(arr) 
          var sum = arr.reduce(function(a, b) return a + +new Date('1970T' + b + 'Z'); , 0);
          return new Date(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']) );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 22:23









          SlaiSlai

          15.5k32436




          15.5k32436





















              0














              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
              ]


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


              return Math.floor( sum/values.length );


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








              share|improve this answer





























                0














                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
                ]


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


                return Math.floor( sum/values.length );


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








                share|improve this answer



























                  0












                  0








                  0







                  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
                  ]


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


                  return Math.floor( sum/values.length );


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








                  share|improve this answer















                  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
                  ]


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


                  return Math.floor( sum/values.length );


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








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


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


                  return Math.floor( sum/values.length );


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





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


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


                  return Math.floor( sum/values.length );


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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 7 at 22:56

























                  answered Mar 7 at 18:57









                  Steven StarkSteven Stark

                  666618




                  666618



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049727%2ffind-time-average-of-hhmmss%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived