总结angularjs应用程序的json数组中的时序差异是错误的

时间:2023-01-04 21:22:50

I have the following array of json objects and function to get total Hours count.

我有以下数组的json对象和函数来获取总小时数。

$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
  }
];

var toatlWorkingHrsCnt = getTotalHoursCount();    

 function getTotalHoursCount() {
        var totalHrs = 0.0;
        for (var i = 0; i < $scope.workingHrsList .length; i++) {
            totalHrs = totalHrs + (parseFloat($scope.workingHrsList [i]["uniqueEndTime"]) - parseFloat($scope.workingHrsList [i]["uniqueStartTime"]));
            $scope.workingHrsList [i]["diffHrs"] = (parseFloat($scope.workingHrsList [i]["uniqueEndTime"]) - parseFloat($scope.workingHrsList [i]["uniqueStartTime"])) + "";
            console.log("End : " + parseFloat($scope.workingHrsList [i]["uniqueEndTime"]));
            console.log("Start : " + parseFloat($scope.workingHrsList [i]["uniqueStartTime"]));
            console.log("Row Hrs with Index " + i + "  : " + totalHrs);
        }
        console.log("Total Hours : " + totalHrs);
        return (totalHrs);
    }

so that I can check

这样我就可以检查了

if(toatlWorkingHrsCnt == "24.00" ) {
console.log("Payable");
} else {
console.log("Not Payable");
}

Even though manually, we can clearly see that the difference in the timings is adding up to 24hrs, but the function is not giving that result. Can any one help me to fix the issue?

即使手动,我们也可以清楚地看到时间上的差异加起来是24小时,但功能并没有给出结果。任何人都可以帮我解决这个问题吗?

2 个解决方案

#1


1  

you are using 24hrs format, then try this,

你使用24小时格式,然后试试这个,

$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
                    }
                ];



function getTotalHoursCount() {
        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 time
            if(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;
    }

#2


1  

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:

我认为你的问题是你处理时差的方式。对于行[1],您的结束时间早于开始时间。因此,您需要在24小时内添加帐户。以下是循环的一些更新逻辑:

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/

这是一个简单的工作示例。 https://jsfiddle.net/L4oq1p85/

#1


1  

you are using 24hrs format, then try this,

你使用24小时格式,然后试试这个,

$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
                    }
                ];



function getTotalHoursCount() {
        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 time
            if(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;
    }

#2


1  

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:

我认为你的问题是你处理时差的方式。对于行[1],您的结束时间早于开始时间。因此,您需要在24小时内添加帐户。以下是循环的一些更新逻辑:

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/

这是一个简单的工作示例。 https://jsfiddle.net/L4oq1p85/