如何从AngularJS中的当前日期中减去日期

时间:2016-10-14 06:49:36

标签: angularjs date controller

我正在尝试计算员工在我的申请中的经历。我打算通过从'当前日期'中减去'careerStartedOn'这样做。

这是我的控制器代码:

myApp.controller('getAllBenchersController', ['$scope', 'employeeTalentPoolServices', 'dataTable', '$window', '$timeout', function ($scope, employeeTalentPoolServices, dataTable, $window, $timeout) {
    employeeTalentPoolServices.getAllBenchers().then(function (result) {
     var mainData = result.data;
        $scope.date = new Date();
         $scope.blockEmployee = function (id) {

            employeeTalentPoolServices.blockEmployee(id);
            $scope.showhide = false;
        }
         });

    employeeTalentPoolServices.getCustomerAccounts().then(function (result) {
        $scope.accountData = result.data;
        });

}]);

Html:

<div class="widget-content table-container" ng-controller="getAllBenchersController">
<table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered">
                    <tr ng-repeat="employee in data">
      <td data-title="'Experience'" sortable="'Experience'" filter="{ 'account': 'text' }">
                         {{employee.careerStartedOn | date:myApp.dateFormat}}
                        </td>
    </tr>
                </table>

在HTML中,如果我拨打{{employee.careerStartedOn | date:myApp.dateFormat}},我将获得'careerStartedOn'日期,当我致电{{date | date:myApp.dateFormat}}时,我将获得当前日期。

我需要从当前的日期中减去 careerStartedOn 日期,然后将其显示在<td>中。我是一个更新鲜,无法找到解决方案来做到这一点。

任何人都可以帮我解决这个问题吗?

提前致谢...

2 个答案:

答案 0 :(得分:2)

您可以使用此功能计算年/月的经验

$scope.CalDate = function(date1,date2) {
    var diff = Math.floor(date1.getTime() - date2.getTime());
    var secs = Math.floor(diff/1000);
    var mins = Math.floor(secs/60);
    var hours = Math.floor(mins/60);
    var days = Math.floor(hours/24);
    var months = Math.floor(days/31);
    var years = Math.floor(months/12);
    months=Math.floor(months%12);
    days = Math.floor(days%31);
    hours = Math.floor(hours%24);
    mins = Math.floor(mins%60);
    secs = Math.floor(secs%60); 
    var message = ""; 
    if(days<=0){
    message += secs + " sec "; 
    message += mins + " min "; 
    message += hours + " hours "; 
    }else{
            if(years>0){
            message += years + " years ";    
        }
        if(months>0 || years>0){
            message += months + " months ";
        }
        message += days + " days";
    }
    return message
};


$scope.getExp = function(date)
{
    date = new Date($filter('date')(date, "yyyy/MM/dd"));
  var currdate = new Date($filter('date')(new Date(), "yyyy/MM/dd"));

  var exp = $scope.CalDate(currdate,date);
  return exp;
}

HTML

<table ng-table="talentPoolList" show-filter="true" class="table table-striped table-bordered">
 <th>
   <tr>
     <td>Career Started On</td>
     <td align="center">Experience</td>
   </tr>
 </th>
 <tr ng-repeat="employee in data">
  <td>
     {{employee.careerStartedOn | date: 'yyyy/MM/dd'}}
   </td>
   <td align="center">
   {{getExp(employee.careerStartedOn)}}
   </td>
</tr>
</table>

如果您不需要显示天数,则可以删除此行

message += days + " days";

<强> FULL EXAMPLE

Source

答案 1 :(得分:1)

您可以在HTML中执行以下操作,以显示多年的体验

           <tr ng-repeat="employee in data">
  <td data-title="'Experience'" sortable="'Experience'" filter="{ 'account': 'text' }">
                     {{getExp(employee.careerStartedOn)}}
                    </td>

在你的JS中

    $scope.getExp = function(str) {
    var today = new Date();
    var startDate = new Date(str);
    var exp = today.getFullYear() - startDate.getFullYear();
    var m = today.getMonth() - startDate.getMonth();
    return exp;
  };
相关问题