如何在AngularJS中减去当前日期的日期 [英] How to subtract a date from current date in AngularJS

查看:288
本文介绍了如何在AngularJS中减去当前日期的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算我的应用程序中员工的体验。我计划通过从当前日期减去careerStartedOn来实现。



这是我的控制器代码:



$ pre> myApp.controller('getAllBenchersController',['$ scope','employeeTalentPoolServices','dataTable','$ window','$ timeout' 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:

 code>< div class =widget-content table-containerng-controller =getAllBenchersController> 
< table ng-table =talentPoolListshow-filter =trueclass =table table-striped table-bordered>
< trng-repeat =数据中的员工>
< td data-title ='Experience'sortable ='Experience'filter ={'account':'text'}>
{{employee.careerStartedOn | date:myApp.dateFormat}}
< / td>
< / tr>
< / table>

在HTML中,如果我调用 {{employee.careerStartedOn |日期:myApp.dateFormat}} 我将获得'careerStartedOn'日期,当我调用 {{date |日期:myApp.dateFormat}} 我将获得当前日期。



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



任何人都可以通过代码帮助我实现这一点?



提前感谢

解决方案

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



  $ 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(小时%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;
}
返回消息
};


$ 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 =talentPoolListshow-filter =trueclass =table table-striped table-bordered> 
< th>
< tr>
< td>职业生涯开始< / td>
< td align =center>体验< / td>
< / tr>
< / th>
< trng-repeat =数据中的员工>
< td>
{{employee.careerStartedOn |日期:'yyyy / MM / dd'}}
< / td>
< td align =center>
{{getExp(employee.careerStartedOn)}}
< / td>
< / tr>
< / table>

如果您不需要显示日期,可以删除此行

  message + = days +days; 

完整示例




I am trying to calculate experience of an employee in my application. I was planning to do so by subtracting 'careerStartedOn' from the 'Current date'.

Here's my code for controller :

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>

in HTML, if i call {{employee.careerStartedOn | date:myApp.dateFormat}} i will get the 'careerStartedOn' date and when i call {{date | date:myApp.dateFormat}} i will get the current date.

I need to substract the careerStartedOn date from the current date and then display it in the <td>. I am a fresher and cant find a solution to do this.

Can anyone help me out with the code to achieve this?

Thanks in advance...

解决方案

You can use this function to calculate experience in years/months

$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>

If you don't need days to be displayed you can remove this line

message += days + " days";

FULL EXAMPLE

Source

这篇关于如何在AngularJS中减去当前日期的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆