Find sum of time in an array of time format hh:mm:ss2019 Community Moderator ElectionFind time average of hh:mm:ssCalculate relative time in C#How do I check if an array includes an object in JavaScript?How to append something to an array?How to get the current time in PythonWhat do 'real', 'user' and 'sys' mean in the output of time(1)?How to insert an item into an array at a specific index (JavaScript)?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Unable to get newly inserted Product's Id using After Plugin for Catalog Product save controller method
Why I don't get the wanted width of tcbox?
PTIJ: Where did Achashverosh's years wander off to?
Was World War I a war of liberals against authoritarians?
Would mining huge amounts of resources on the Moon change its orbit?
Weird lines in Microsoft Word
Help with identifying unique aircraft over NE Pennsylvania
If I cast the Enlarge/Reduce spell on an arrow, what weapon could it count as?
PTIJ: Which Dr. Seuss books should one obtain?
is this saw blade faulty?
Naïve RSA decryption in Python
Which partition to make active?
Does convergence of polynomials imply that of its coefficients?
How can an organ that provides biological immortality be unable to regenerate?
Why doesn't the fusion process of the sun speed up?
How can a new country break out from a developed country without war?
Gauss brackets with double vertical lines
When should a starting writer get his own webpage?
How old is Nick Fury?
Why is indicated airspeed rather than ground speed used during the takeoff roll?
Hot air balloons as primitive bombers
PTIJ: Why do we make a Lulav holder?
How to determine the greatest d orbital splitting?
How to test the sharpness of a knife?
Find sum of time in an array of time format hh:mm:ss
2019 Community Moderator ElectionFind time average of hh:mm:ssCalculate relative time in C#How do I check if an array includes an object in JavaScript?How to append something to an array?How to get the current time in PythonWhat do 'real', 'user' and 'sys' mean in the output of time(1)?How to insert an item into an array at a specific index (JavaScript)?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
I have this code that works to find the sum properly for format hh:mm
I am having hard time converting this to format hh:mm:ss
Obviously everything should be rounded up correctly.
this is the format of the array i am looking for to work with but code obviously works only for ["00", "10"] as an example
const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]
var mins = 0;
var hrs = 0;
if (array == null)
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
else
$.each(array, function(key)
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
);
console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
t1
and t2
in this case are just 2 separate values to be added together then there is an array of bunch of these. Both should work right.
please help me add properly seconds in here too.
javascript time
add a comment |
I have this code that works to find the sum properly for format hh:mm
I am having hard time converting this to format hh:mm:ss
Obviously everything should be rounded up correctly.
this is the format of the array i am looking for to work with but code obviously works only for ["00", "10"] as an example
const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]
var mins = 0;
var hrs = 0;
if (array == null)
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
else
$.each(array, function(key)
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
);
console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
t1
and t2
in this case are just 2 separate values to be added together then there is an array of bunch of these. Both should work right.
please help me add properly seconds in here too.
javascript time
1
If you're dealing with time, have a look at moment.js - it has all the bits you need. momentjs.com/docs
– Rastalamm
Mar 7 at 19:01
thank you for your suggestion but i am just asking to help me edit the code that i already have to work with seconds as well.. thanks
– coder
Mar 7 at 19:04
do you have some data in text form and the wanted result?
– Nina Scholz
Mar 7 at 19:05
2
btw, it looks like the last question without the average part.
– Nina Scholz
Mar 7 at 19:06
just updated. thanks for letting me know
– coder
Mar 7 at 19:13
add a comment |
I have this code that works to find the sum properly for format hh:mm
I am having hard time converting this to format hh:mm:ss
Obviously everything should be rounded up correctly.
this is the format of the array i am looking for to work with but code obviously works only for ["00", "10"] as an example
const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]
var mins = 0;
var hrs = 0;
if (array == null)
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
else
$.each(array, function(key)
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
);
console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
t1
and t2
in this case are just 2 separate values to be added together then there is an array of bunch of these. Both should work right.
please help me add properly seconds in here too.
javascript time
I have this code that works to find the sum properly for format hh:mm
I am having hard time converting this to format hh:mm:ss
Obviously everything should be rounded up correctly.
this is the format of the array i am looking for to work with but code obviously works only for ["00", "10"] as an example
const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]
var mins = 0;
var hrs = 0;
if (array == null)
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
else
$.each(array, function(key)
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
);
console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
t1
and t2
in this case are just 2 separate values to be added together then there is an array of bunch of these. Both should work right.
please help me add properly seconds in here too.
const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]
var mins = 0;
var hrs = 0;
if (array == null)
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
else
$.each(array, function(key)
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
);
console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
const array = [
["00", "00", "09"],
["00", "00", "09"],
["00", "00", "04"],
["00", "00", "02"],
["00", "00", "21"],
["00", "00", "14"],
["00", "00", "26"],
["00", "00", "02"],
["00", "00", "14"],
["00", "00", "10"],
["00", "00", "48"],
["00", "00", "12"],
["00", "00", "09"],
["00", "00", "09"]
]
var mins = 0;
var hrs = 0;
if (array == null)
mins = Number(t1[1]) + Number(t2[1]);
hrs = Math.floor(parseInt(mins / 60));
hrs = Number(t1[0]) + Number(t2[0]) + hrs;
else
$.each(array, function(key)
mins += Number(array[key][1]);
hrs = Math.floor(parseInt(mins / 60));
hrs += Number(array[key][0]) + hrs;
);
console.log(mins, hrs)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
javascript time
javascript time
edited Mar 8 at 6:16
adiga
11.3k62545
11.3k62545
asked Mar 7 at 19:00
codercoder
819
819
1
If you're dealing with time, have a look at moment.js - it has all the bits you need. momentjs.com/docs
– Rastalamm
Mar 7 at 19:01
thank you for your suggestion but i am just asking to help me edit the code that i already have to work with seconds as well.. thanks
– coder
Mar 7 at 19:04
do you have some data in text form and the wanted result?
– Nina Scholz
Mar 7 at 19:05
2
btw, it looks like the last question without the average part.
– Nina Scholz
Mar 7 at 19:06
just updated. thanks for letting me know
– coder
Mar 7 at 19:13
add a comment |
1
If you're dealing with time, have a look at moment.js - it has all the bits you need. momentjs.com/docs
– Rastalamm
Mar 7 at 19:01
thank you for your suggestion but i am just asking to help me edit the code that i already have to work with seconds as well.. thanks
– coder
Mar 7 at 19:04
do you have some data in text form and the wanted result?
– Nina Scholz
Mar 7 at 19:05
2
btw, it looks like the last question without the average part.
– Nina Scholz
Mar 7 at 19:06
just updated. thanks for letting me know
– coder
Mar 7 at 19:13
1
1
If you're dealing with time, have a look at moment.js - it has all the bits you need. momentjs.com/docs
– Rastalamm
Mar 7 at 19:01
If you're dealing with time, have a look at moment.js - it has all the bits you need. momentjs.com/docs
– Rastalamm
Mar 7 at 19:01
thank you for your suggestion but i am just asking to help me edit the code that i already have to work with seconds as well.. thanks
– coder
Mar 7 at 19:04
thank you for your suggestion but i am just asking to help me edit the code that i already have to work with seconds as well.. thanks
– coder
Mar 7 at 19:04
do you have some data in text form and the wanted result?
– Nina Scholz
Mar 7 at 19:05
do you have some data in text form and the wanted result?
– Nina Scholz
Mar 7 at 19:05
2
2
btw, it looks like the last question without the average part.
– Nina Scholz
Mar 7 at 19:06
btw, it looks like the last question without the average part.
– Nina Scholz
Mar 7 at 19:06
just updated. thanks for letting me know
– coder
Mar 7 at 19:13
just updated. thanks for letting me know
– coder
Mar 7 at 19:13
add a comment |
4 Answers
4
active
oldest
votes
You could get the seconds add all values and build a new string of the sum.
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
ES5
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
With splitted arrays.
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
that ist1
abdt2
?
– Nina Scholz
Mar 7 at 19:31
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
then omitsplit(':')
.
– Nina Scholz
Mar 7 at 19:34
|
show 7 more comments
Like one of the comments mentioned, moment
is a popular npm package for dealing with time. You can parse the times in your array data structure using the formatting options of moment:
const array = [
[0, 0, 9],
[0, 2, 3],
[1, 2, 3],
...
];
const dates = array.map((date) =>
return moment(`$date[0]-$date[1]-$date[2]`, 'hh-mm-ss');
// make sure you account for 12 or 24 hour clocks, not sure which you are using
);
const sum = dates.reduce((date, total) => return total.add(date); , moment.duration(0));
add a comment |
It's nearly the same math, just applied one more time for seconds -> minutes.
seconds = t1[2] + t2[2];
mins = parseInt(seconds / 60);
seconds = seconds % 60;
mins += t1[1] + t2[1];
hrs = parseInt(minutes / 60);
mins = mins % 60;
hrs += t1[0] + t2[0];
I'm assuming you already know how to convert hh:mm:ss
to an appropriate array by simply using split
and Number
, but just in case:
t1 = time1.split(':').map(part => Number(part));
t2 = time2.split(':').map(part => Number(part));
add a comment |
You could use a reducer function to total the corresponding array elements and then amend the result.
var timeArrays = [
['00', '52', '00'], ['00', '08', '00'], ['00', '00', '40'],
['00', '00', '01'], ['00', '00', '20']
];
function totalTimeAsString(timeArray)
var result = ['00', '00', '00'];
var reducer = function (acc, current)
return [
Number(acc[0]) + Number(current[0]),
Number(acc[1]) + Number(current[1]),
Number(acc[2]) + Number(current[2])
];
;
var [hh, mm, ss] = timeArray.reduce(reducer, result);
// seconds
if (ss > 59)
mm += Math.floor(ss / 60);
ss = ss % 60;
result[2] = ((ss < 10) ? '0' : '') + ss;
// minutes
if (mm > 59)
hh += Math.floor(mm / 60);
mm = mm % 60;
result[1] = ((mm < 10) ? '0' : '') + mm;
// hours
result[0] = ((hh < 10) ? '0' : '') + hh;
return result.join(':');
console.log(totalTimeAsString(timeArrays));
// "01:01:01"
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051019%2ffind-sum-of-time-in-an-array-of-time-format-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
You could get the seconds add all values and build a new string of the sum.
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
ES5
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
With splitted arrays.
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
that ist1
abdt2
?
– Nina Scholz
Mar 7 at 19:31
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
then omitsplit(':')
.
– Nina Scholz
Mar 7 at 19:34
|
show 7 more comments
You could get the seconds add all values and build a new string of the sum.
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
ES5
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
With splitted arrays.
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
that ist1
abdt2
?
– Nina Scholz
Mar 7 at 19:31
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
then omitsplit(':')
.
– Nina Scholz
Mar 7 at 19:34
|
show 7 more comments
You could get the seconds add all values and build a new string of the sum.
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
ES5
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
With splitted arrays.
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
You could get the seconds add all values and build a new string of the sum.
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
ES5
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
With splitted arrays.
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
function sumTime(t1, t2, array = [])
var times = [3600, 60, 1],
sum = [t1, t2, ...array]
.map(s => s.split(':').reduce((s, v, i) => s + times[i] * v, 0))
.reduce((a, b) => a + b, 0);
return times
.map(t => [Math.floor(sum / t), sum %= t][0])
.map(v => v.toString().padStart(2, 0))
.join(':');
console.log(sumTime("01:02:03", "04:57:57"));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
function sumTime(t1, t2, array) [])
.map(function (s)
return s
.split(':') // omit this line for splitted values ['02', '04', '45']
.reduce(function (s, v, i)
return s + times[i] * v;
, 0);
)
.reduce(function (a, b) return a + b; , 0);
return times
.map(function (t)
var value = Math.floor(sum / t);
sum %= t;
return value;
)
.map(function (v)
return v.toString().padStart(2, 0);
)
.join(':');
console.log(sumTime("01:02:03", null));
console.log(sumTime(null, "04:57:57"));
console.log(sumTime(null, null, ["01:02:03", "04:57:57"]));
console.log(sumTime("00:00:09", "00:00:09", ["00:00:04", "00:00:02", "00:00:21", "00:00:14", "00:00:26", "00:00:02", "00:00:14", "00:00:10", "00:00:48", "00:00:12", "00:00:09", "00:00:09"]));
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
function sumTime(t1, t2, array)
var times = [3600, 60, 1],
sum = [t1, t2].filter(Boolean).concat(array
console.log(sumTime(["01", "02", "03"], null));
console.log(sumTime(null, ["04", "57", "57"]));
console.log(sumTime(null, null, [["01", "02", "03"], ["04", "57", "57"]]));
console.log(sumTime(["00", "00", "09"], ["00", "00", "09"], [["00", "00", "04"], ["00", "00", "02"], ["00", "00", "21"], ["00", "00", "14"], ["00", "00", "26"], ["00", "00", "02"], ["00", "00", "14"], ["00", "00", "10"], ["00", "00", "48"], ["00", "00", "12"], ["00", "00", "09"], ["00", "00", "09"]]));
edited Mar 7 at 20:03
answered Mar 7 at 19:13
Nina ScholzNina Scholz
192k15104176
192k15104176
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
that ist1
abdt2
?
– Nina Scholz
Mar 7 at 19:31
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
then omitsplit(':')
.
– Nina Scholz
Mar 7 at 19:34
|
show 7 more comments
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
that ist1
abdt2
?
– Nina Scholz
Mar 7 at 19:31
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
then omitsplit(':')
.
– Nina Scholz
Mar 7 at 19:34
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
can you please convert this into similar code but without =>
– coder
Mar 7 at 19:26
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
and make sure it works for array and separate t1 and t2 values. Your code for average worked great. so function would be function sumTime(t1, t2, array) and if array is empty then use values from t1 and t2
– coder
Mar 7 at 19:28
that is
t1
abd t2
?– Nina Scholz
Mar 7 at 19:31
that is
t1
abd t2
?– Nina Scholz
Mar 7 at 19:31
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
also...format that i pass the array in is in the description. .. i don't need to split anymore.. it is already split ["00", "00", "09"], so this array of 0 is hours, of 1 is min and of 2 is seconds
– coder
Mar 7 at 19:32
then omit
split(':')
.– Nina Scholz
Mar 7 at 19:34
then omit
split(':')
.– Nina Scholz
Mar 7 at 19:34
|
show 7 more comments
Like one of the comments mentioned, moment
is a popular npm package for dealing with time. You can parse the times in your array data structure using the formatting options of moment:
const array = [
[0, 0, 9],
[0, 2, 3],
[1, 2, 3],
...
];
const dates = array.map((date) =>
return moment(`$date[0]-$date[1]-$date[2]`, 'hh-mm-ss');
// make sure you account for 12 or 24 hour clocks, not sure which you are using
);
const sum = dates.reduce((date, total) => return total.add(date); , moment.duration(0));
add a comment |
Like one of the comments mentioned, moment
is a popular npm package for dealing with time. You can parse the times in your array data structure using the formatting options of moment:
const array = [
[0, 0, 9],
[0, 2, 3],
[1, 2, 3],
...
];
const dates = array.map((date) =>
return moment(`$date[0]-$date[1]-$date[2]`, 'hh-mm-ss');
// make sure you account for 12 or 24 hour clocks, not sure which you are using
);
const sum = dates.reduce((date, total) => return total.add(date); , moment.duration(0));
add a comment |
Like one of the comments mentioned, moment
is a popular npm package for dealing with time. You can parse the times in your array data structure using the formatting options of moment:
const array = [
[0, 0, 9],
[0, 2, 3],
[1, 2, 3],
...
];
const dates = array.map((date) =>
return moment(`$date[0]-$date[1]-$date[2]`, 'hh-mm-ss');
// make sure you account for 12 or 24 hour clocks, not sure which you are using
);
const sum = dates.reduce((date, total) => return total.add(date); , moment.duration(0));
Like one of the comments mentioned, moment
is a popular npm package for dealing with time. You can parse the times in your array data structure using the formatting options of moment:
const array = [
[0, 0, 9],
[0, 2, 3],
[1, 2, 3],
...
];
const dates = array.map((date) =>
return moment(`$date[0]-$date[1]-$date[2]`, 'hh-mm-ss');
// make sure you account for 12 or 24 hour clocks, not sure which you are using
);
const sum = dates.reduce((date, total) => return total.add(date); , moment.duration(0));
answered Mar 7 at 19:08
jakemingollajakemingolla
50739
50739
add a comment |
add a comment |
It's nearly the same math, just applied one more time for seconds -> minutes.
seconds = t1[2] + t2[2];
mins = parseInt(seconds / 60);
seconds = seconds % 60;
mins += t1[1] + t2[1];
hrs = parseInt(minutes / 60);
mins = mins % 60;
hrs += t1[0] + t2[0];
I'm assuming you already know how to convert hh:mm:ss
to an appropriate array by simply using split
and Number
, but just in case:
t1 = time1.split(':').map(part => Number(part));
t2 = time2.split(':').map(part => Number(part));
add a comment |
It's nearly the same math, just applied one more time for seconds -> minutes.
seconds = t1[2] + t2[2];
mins = parseInt(seconds / 60);
seconds = seconds % 60;
mins += t1[1] + t2[1];
hrs = parseInt(minutes / 60);
mins = mins % 60;
hrs += t1[0] + t2[0];
I'm assuming you already know how to convert hh:mm:ss
to an appropriate array by simply using split
and Number
, but just in case:
t1 = time1.split(':').map(part => Number(part));
t2 = time2.split(':').map(part => Number(part));
add a comment |
It's nearly the same math, just applied one more time for seconds -> minutes.
seconds = t1[2] + t2[2];
mins = parseInt(seconds / 60);
seconds = seconds % 60;
mins += t1[1] + t2[1];
hrs = parseInt(minutes / 60);
mins = mins % 60;
hrs += t1[0] + t2[0];
I'm assuming you already know how to convert hh:mm:ss
to an appropriate array by simply using split
and Number
, but just in case:
t1 = time1.split(':').map(part => Number(part));
t2 = time2.split(':').map(part => Number(part));
It's nearly the same math, just applied one more time for seconds -> minutes.
seconds = t1[2] + t2[2];
mins = parseInt(seconds / 60);
seconds = seconds % 60;
mins += t1[1] + t2[1];
hrs = parseInt(minutes / 60);
mins = mins % 60;
hrs += t1[0] + t2[0];
I'm assuming you already know how to convert hh:mm:ss
to an appropriate array by simply using split
and Number
, but just in case:
t1 = time1.split(':').map(part => Number(part));
t2 = time2.split(':').map(part => Number(part));
answered Mar 7 at 19:10
IceMetalPunkIceMetalPunk
954716
954716
add a comment |
add a comment |
You could use a reducer function to total the corresponding array elements and then amend the result.
var timeArrays = [
['00', '52', '00'], ['00', '08', '00'], ['00', '00', '40'],
['00', '00', '01'], ['00', '00', '20']
];
function totalTimeAsString(timeArray)
var result = ['00', '00', '00'];
var reducer = function (acc, current)
return [
Number(acc[0]) + Number(current[0]),
Number(acc[1]) + Number(current[1]),
Number(acc[2]) + Number(current[2])
];
;
var [hh, mm, ss] = timeArray.reduce(reducer, result);
// seconds
if (ss > 59)
mm += Math.floor(ss / 60);
ss = ss % 60;
result[2] = ((ss < 10) ? '0' : '') + ss;
// minutes
if (mm > 59)
hh += Math.floor(mm / 60);
mm = mm % 60;
result[1] = ((mm < 10) ? '0' : '') + mm;
// hours
result[0] = ((hh < 10) ? '0' : '') + hh;
return result.join(':');
console.log(totalTimeAsString(timeArrays));
// "01:01:01"
add a comment |
You could use a reducer function to total the corresponding array elements and then amend the result.
var timeArrays = [
['00', '52', '00'], ['00', '08', '00'], ['00', '00', '40'],
['00', '00', '01'], ['00', '00', '20']
];
function totalTimeAsString(timeArray)
var result = ['00', '00', '00'];
var reducer = function (acc, current)
return [
Number(acc[0]) + Number(current[0]),
Number(acc[1]) + Number(current[1]),
Number(acc[2]) + Number(current[2])
];
;
var [hh, mm, ss] = timeArray.reduce(reducer, result);
// seconds
if (ss > 59)
mm += Math.floor(ss / 60);
ss = ss % 60;
result[2] = ((ss < 10) ? '0' : '') + ss;
// minutes
if (mm > 59)
hh += Math.floor(mm / 60);
mm = mm % 60;
result[1] = ((mm < 10) ? '0' : '') + mm;
// hours
result[0] = ((hh < 10) ? '0' : '') + hh;
return result.join(':');
console.log(totalTimeAsString(timeArrays));
// "01:01:01"
add a comment |
You could use a reducer function to total the corresponding array elements and then amend the result.
var timeArrays = [
['00', '52', '00'], ['00', '08', '00'], ['00', '00', '40'],
['00', '00', '01'], ['00', '00', '20']
];
function totalTimeAsString(timeArray)
var result = ['00', '00', '00'];
var reducer = function (acc, current)
return [
Number(acc[0]) + Number(current[0]),
Number(acc[1]) + Number(current[1]),
Number(acc[2]) + Number(current[2])
];
;
var [hh, mm, ss] = timeArray.reduce(reducer, result);
// seconds
if (ss > 59)
mm += Math.floor(ss / 60);
ss = ss % 60;
result[2] = ((ss < 10) ? '0' : '') + ss;
// minutes
if (mm > 59)
hh += Math.floor(mm / 60);
mm = mm % 60;
result[1] = ((mm < 10) ? '0' : '') + mm;
// hours
result[0] = ((hh < 10) ? '0' : '') + hh;
return result.join(':');
console.log(totalTimeAsString(timeArrays));
// "01:01:01"
You could use a reducer function to total the corresponding array elements and then amend the result.
var timeArrays = [
['00', '52', '00'], ['00', '08', '00'], ['00', '00', '40'],
['00', '00', '01'], ['00', '00', '20']
];
function totalTimeAsString(timeArray)
var result = ['00', '00', '00'];
var reducer = function (acc, current)
return [
Number(acc[0]) + Number(current[0]),
Number(acc[1]) + Number(current[1]),
Number(acc[2]) + Number(current[2])
];
;
var [hh, mm, ss] = timeArray.reduce(reducer, result);
// seconds
if (ss > 59)
mm += Math.floor(ss / 60);
ss = ss % 60;
result[2] = ((ss < 10) ? '0' : '') + ss;
// minutes
if (mm > 59)
hh += Math.floor(mm / 60);
mm = mm % 60;
result[1] = ((mm < 10) ? '0' : '') + mm;
// hours
result[0] = ((hh < 10) ? '0' : '') + hh;
return result.join(':');
console.log(totalTimeAsString(timeArrays));
// "01:01:01"
answered Mar 7 at 20:06
Dan NagleDan Nagle
1,9221817
1,9221817
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051019%2ffind-sum-of-time-in-an-array-of-time-format-hhmmss%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
If you're dealing with time, have a look at moment.js - it has all the bits you need. momentjs.com/docs
– Rastalamm
Mar 7 at 19:01
thank you for your suggestion but i am just asking to help me edit the code that i already have to work with seconds as well.. thanks
– coder
Mar 7 at 19:04
do you have some data in text form and the wanted result?
– Nina Scholz
Mar 7 at 19:05
2
btw, it looks like the last question without the average part.
– Nina Scholz
Mar 7 at 19:06
just updated. thanks for letting me know
– coder
Mar 7 at 19:13