Currently, I am facing an issue with retrieving the user's work duration from a database column stored in minutes. My goal is to convert this value to HH:mm format to calculate the total hours worked per week, but I'm encountering obstacles.
<td><strong>{{ Math.floor(teamUniqueHoursTotalPerUser[entry] / 1000 / 60 / 60) + 'h ' + Math.round(teamUniqueHoursTotalPerUser[entry] / 1000 / 60 % 60) + 'm' }}</strong></td>
I struggle with math, so my aim is to tackle this task using only Math libraries to enhance my skills. Currently, when there is no value present, I receive a NaN error. Instead, I would like it to display as Total: 0h 0m when no value exists.
Within the controller:
$scope.teamTimeInfoForCurrentWeek = {};
if (data != null) {
var uniqueNamesArray = [];
var uniqueHoursWorkedCurrentWeekPerUser = {};
for (i = 0; i < data.length; i++) {
var newUser = 0;
data[i].Full_Name = data[i].Employee_Last_Name + ', ' + data[i].Employee_First_Name;
if (uniqueNamesArray.indexOf(data[i].Full_Name) == -1) {
uniqueNamesArray.push(data[i].Full_Name);
newUser = 1;
}
if (data[i].Hours_Worked !== null && data[i].Hours_Worked >= 0) {
data[i].Hours_Worked = moment.duration(data[i].Hours_Worked, 'minutes');
if (newUser == 1) {
uniqueHoursWorkedCurrentWeekPerUser[data[i].Full_Name] = moment.duration(data[i].Hours_Worked, 'minutes');
}
else {
uniqueHoursWorkedCurrentWeekPerUser[data[i].Full_Name] += moment.duration(data[i].Hours_Worked, 'minutes');
}
}
if (data[i].Lunch_Duration !== null && data[i].Lunch_Duration >= 0) {
data[i].Lunch_Duration = moment.duration(data[i].Lunch_Duration, 'minutes');
}
}
$scope.teamTimeInfoForCurrentWeek = data;
$scope.teamHoursWorkedForCurrentWeek = uniqueHoursWorkedCurrentWeekPerUser;
$scope.teamNameInfoForCurrentWeek = uniqueNamesArray;
Despite attempting to incorporate moment.js, I encountered difficulties with values exceeding 24 hours when trying to create a moment duration (minuteValue, 'minutes').
Any assistance provided would be greatly appreciated. JavaScript remains a challenging area for me.