angularjs 中的日期减法

Subtraction of Date in angularjs

我想减去 angularjs 中从数据库中获取的两个日期。该代码适用于所有值,除了从数据库中获取的日期之间的减法...我使用 PHP 和 MySQL 谁能告诉我如何做这个减法。这是代码。提前致谢

<html>
<head>
    <script src="angular.js"></script>
    <script>
        var app= angular.module("myapp",[]);
                        app.controller("mycontroller",function($scope,$http){
                        $http.get("select.php").success(function(response)
                         {
                            $scope.names = response.data.records;});

                        });

    </script>


</head>
<body ng-app="myapp"   ng-controller="mycontroller">
    <table border=1>
        <tr>
            <th>ID </th>
            <th>Name of employee</th>
            <th>Start Date </th>
            <th>End Date </th>
            <th>No of days Worked </th>

        </tr>
        <tr  ng-repeat="emp in names">
            <td>{{emp.id}}</td>
            <td>{{emp.name}}</td>
            <td>{{emp.stDate}}</td>
            <td>{{emp.enDate}}</td>
            <td>{{emp.stDate-emp.enDate}}</td>

        </tr>

    </table>

</body>

您只需要编写一个控制器函数,它会进行所有计算并return 返回结果。

$scope.calDate = function(date1, date2){
   var dateOut1 = new Date(date1); // it will work if date1 is in ISO format
   var dateOut2 = new Date(date2);
   var timeDiff = Math.abs(dateOut2.getTime() - dateOut1.getTime());
   var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
   return diffDays;
};

并在 HTML

<tr  ng-repeat="emp in names">
            <td>{{emp.id}}</td>
            <td>{{emp.name}}</td>
            <td>{{emp.stDate}}</td>
            <td>{{emp.enDate}}</td>
            <td>{{calDate(emp.stDate,emp.endDate)}}</td>
</tr>