Say, I have the values 01:00
, 01:00
, 00:30
, 00:30
, 01:00
, 01:00
, 00:30
, 00:30
, 01:00
in duration format (HH:MM) in cell E2
to E10
.
The formula =SUM(E2:E10)
returns 07:00
which is correct, but my following custom spreadsheet function written in Google Apps Script returns 07:54
which is incorrect.
How can I correctly calculate sum of the durations using a custom function?
function totalDuration() {
var result = 0;
var durations = SpreadsheetApp.getActiveSheet().getRange("E2:E10").getValues();
for(var i = 0; i < durations.length; i++) {
var date = new Date(durations[i]);
result += (date.getHours() * 60) + date.getMinutes();
}
return result / 60 / 24;
}