My data table is displaying records including a column for hours spent and a row showing the total sum of those hours. While the hours are being added correctly, the minutes display as NaN
, such as 52:Nan
. Can someone assist me in resolving this issue?
component.ts
empPageChange(event) {
let list = this.timetables.slice(event.first, event.first + event.rows);
if (list.length > 0) {
this.totalHoursSpent = HelperService.addTimes(list, 'HoursSpent');
} else {
this.totalHoursSpent = 0;
}
}
helper.service.ts:
static addTimes(timeMap, columnName: string) {
let totalH = 0;
let totalM = 0;
// First simply adding all of it together, total hours and total minutes
for (let x in timeMap) {
if (x) {
let timeArray = timeMap[x][columnName].split(':');
let hour = timeArray[0];
let minutes = timeArray[1];
totalH += parseInt(hour, 10);
totalM += parseInt(minutes, 10);
}
}
// If the minutes exceed 60
if (totalM >= 60) {
// Divide minutes by 60 and add result to hours
totalH += Math.floor(totalM / 60);
// Add remainder of totalM / 60 to minutes
totalM = totalM % 60;
}
return `${totalH}:${
totalM.toString().length === 1 ? `0${totalM}` : totalM
}`;
}