How to Sum N Number of Time (Hh:Mm Format)

How to sum N number of time (HH:MM Format)?

this should do what you are looking for:

$times is the array of times and you can add how many time you want

$times = array();

$times[] = "12:59";
$times[] = "0:58";
$times[] = "0:02";

// pass the array to the function
echo AddPlayTime($times);

function AddPlayTime($times) {
$minutes = 0; //declare minutes either it gives Notice: Undefined variable
// loop throught all the times
foreach ($times as $time) {
list($hour, $minute) = explode(':', $time);
$minutes += $hour * 60;
$minutes += $minute;
}

$hours = floor($minutes / 60);
$minutes -= $hours * 60;

// returns the time already formatted
return sprintf('%02d:%02d', $hours, $minutes);
}

EDIT

I edited the code with the right names of the variables. It is more correct now.

hope this helps :-)

Find sum of time in an array of time format hh:mm:ss

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"]));

addition of hours in HH:MM using jquery

First parse the strings as floats, and then add them up

var sum = [t1,t2,t3,t4,t5].map(function(x) {
return parseFloat( x.replace(':','.') );
}).reduce(function(a,b) { return a + b; });

that leaves you with 11.8, so we need to check if that last number is more than .6 and if it is, add 1 to the total, subtract .6 from the decimal and add the remainder

var left = (sum - parseInt(sum, 10)).toFixed(2);

if ( left >= 0.6 ) sum = parseInt( (sum += 1), 10 ) + parseFloat((left - 0.6).toFixed(2));

Now you have the number 12.2, if you want 12:20 you parse it back to a string add pad it

var parts = sum.toString().split('.');

parts[1] = parts[1] < 10 ? parts[1] + '0' : parts[1];

sum = parts[0] + ':' + parts[1];

FIDDLE (another one)

Note the heavy use of parseFloat and toFixed, that's to avoid annoying errors with floating point numbers

How to get sum of strings in JavaScript? (time format: 00:00:00)

Please try with this code.

let arr = ["00:00:30", "00:20:00", "01:00:10", "05:10:15"]
let sum = "";
for (let i = 0; i < arr.length; i++) {
if(i == 0){
sum = arr[i];
continue;
}else{
var a = sum.split(":");
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var b = arr[i].split(":");
var seconds2 = (+b[0]) * 60 * 60 + (+b[1]) * 60 + (+b[2]);
var date = new Date(1970,0,1);
date.setSeconds(seconds + seconds2);
sum = date.toTimeString().replace(/.*(\d{2}:\d{2}:\d{2}).*/, "$1");
}
}
console.log(sum);

Calculating total time from HH:MM format

You can use Moment.js (https://momentjs.com/) to calculate this:

response = {
startTime: '09:00 AM',
endTime: '02:00 PM'
};

var momentStartTime = moment(response.startTime, 'hh:mm a');
var momentEndTime = moment(response.endTime, 'hh:mm a');

console.log('Time difference (minutes): ', momentEndTime.diff(momentStartTime, 'minutes'));

var html = 'Time difference output: <br/>';
html += 'Difference (hours): ' + momentEndTime.diff(momentStartTime, 'hours') + '<br/>';
html += 'Difference (minutes): ' + momentEndTime.diff(momentStartTime, 'minutes') + '<br/>';
html += 'Difference (seconds): ' + momentEndTime.diff(momentStartTime, 'seconds') + '<br/>';

$('#output').html(html);

I've created a JS Fiddle for this here:

https://jsfiddle.net/rrLdjjjp/1/

C# Method to sum hh:mm data ???

Convert the strings to TimeSpans and then call the .Add method.

TimeSpan s1 = TimeSpan.Parse("0:15");
TimeSpan s2 = TimeSpan.Parse("0:45");

TimeSpan s3 = s1 + s2;

// not tested; should work.

Ref: http://msdn.microsoft.com/en-us/library/system.timespan.aspx

How to sum time string in an array?

You can treat time as moment durations that can be summed up:

const any = ['7:20', '7:52', '5:03', '1:01', '9:02', '6:00'];

const sum = any.reduce((acc, time) => acc.add(moment.duration(time)), moment.duration());

console.log([Math.floor(sum.asHours()), sum.minutes()].join(':'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>


Related Topics



Leave a reply



Submit