Summing Up Of Difference In Timings In Json Array Of Angularjs Application Is Going Wrong
I have the following array of json objects and function to get total Hours count. $scope.workingHrsList = [ { 'uniqueStartTime': '4.00', //4 AM 'uniqueEndTime': '16.45
Solution 1:
you are using 24hrs format, then try this,
$scope.workingHrsList = [
{
"uniqueStartTime": "4.00", //4 AM"uniqueEndTime": "16.45"//4.45 PM
},
{
"uniqueStartTime": "16.45", //4.45 PM"uniqueEndTime": "3.15"// 3.15 AM
},
{
"uniqueStartTime": "3.15", //3.15 AM"uniqueEndTime": "4.00"// 4.00 AM
}
];
functiongetTotalHoursCount() {
var totalHrs = 0, uEndTime = 0, uStartTime = 0, diff = 0;
for (var i = 0; i < $scope.workingHrsList .length; i++) {
uEndTime = parseFloat($scope.workingHrsList[i].uniqueEndTime);
uStartTime = parseFloat($scope.workingHrsList[i].uniqueStartTime);
//always start greater than end time if not //it means they worked continued for next day, //so we need to 24 hrs with end timeif(uEndTime > uStartTime){
diff = uEndTime - uStartTime;
}
else{
diff = (uEndTime + 24) - uStartTime;
}
totalHrs += diff;
$scope.workingHrsList[i].diffHrs = diff;
console.log("End : " + uEndTime);
console.log("Start : " + uStartTime);
console.log("Row Hrs with Index " + i + " : " + totalHrs);
}
console.log("Total Hours : " + totalHrs);
return totalHrs;
}
Solution 2:
I think your problem is the way you are handling your time differences. For row[1] your end time is before your start time. So you need to add on 24 hours to account for that. Here is some updated logic for your loop:
var row = workingHrsList[i];
var endTime = parseFloat(row["uniqueEndTime"]);
var startTime = parseFloat(row["uniqueStartTime"])
var diff = endTime - startTime;
if(diff < 0) {
diff += 24;
console.log("24 hours added");
}
totalHrs += diff;
Here is a simple working example. https://jsfiddle.net/L4oq1p85/
Post a Comment for "Summing Up Of Difference In Timings In Json Array Of Angularjs Application Is Going Wrong"