【问题标题】:How to subtract a date from current date in AngularJS如何从AngularJS中的当前日期中减去一个日期
【发布时间】:2016-10-14 06:49:36
【问题描述】:

我正在尝试在我的应用程序中计算员工的经验。我打算通过从“当前日期”中减去“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}} 时,我会得到当前日期。

我需要从当前 date 中减去 careerStartedOn 日期,然后将其显示在 &lt;td&gt; 中。我是个新手,找不到解决方案。

谁能帮我写代码来实现这个目标?

提前谢谢...

【问题讨论】:

  • 你想展示什么?您可以为此使用moment.js
  • 我想显示计算结果 (CurrentDate - CareerStartedOn 。差异应该是年/月数
  • @DanielShillcock - 它在您提到的帖子中使用 javascript。我想知道如何使用控制器在 AngularJS 中实现这一点。
  • @pro.mean - 我想显示计算的结果 (CurrentDate - CareerStartedOn 。差异应该是年/月。你能告诉我一个如何使用的例子吗? moment.js?我不知道在哪里做。在控制器中还是在 HTML 中?

标签: angularjs date controller


【解决方案1】:

你可以使用这个函数来计算年/月的经验

$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

【讨论】:

  • 我在“careerStartedOn”中的日期格式是“2016-10-03T13:44:00.587”。此外,您正在对函数中的日期进行硬编码。如何从我的 API {{employee.careerStartedOn}} 中获取它
  • datedate object 过滤成string。您需要通过new Date("YOUR STRING DATE") 将其转换回date object。因此,无论您在应用date filter 后从employee.careerStartedOn 获得什么日期,都将格式化的日期传递给new Date("PASS IT HERE")。并使用 CalDate() 计算经验
  • 好的,但由于我仍处于学习阶段,我不知道该怎么做。你能在你的答案中显示出来吗? Tnx
  • @Phoenix 我已经更新了我的答案。我采用了一个假人data array,其中包含一些值。另请参阅更新的链接
  • $scope.getExp = function (date) { 正在获取值 (date = "2016-10-03T13:44:00.587)。之后它没有执行语句:var currdate = new日期($filter('date')(new Date(), "yyyy/MM/dd"));
【解决方案2】:

您可以在 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;
  };

【讨论】:

  • s18.postimg.org/3l50bk8nt/debug.jpg 我试过了,但它没有执行 var diff= date.getTime() - startDate.getTime();请检查屏幕截图以获取信息
  • 您正在将日期字符串发送到函数中,而不是尝试发送日期对象
  • 错误是 :: "[$interpolate:interr] Can't interpolate: {{getExp(employee.careerStartedOn)}} TypeError: startDate.getTime is not a function
  • @Phoenix 我已经更新了答案,现在试试!错误是因为您将日期作为字符串对象而不是日期发送,这就是没有 getTime () 函数的原因。
  • 好的,我希望将结果转换为年/月/日以使其成为准确的数量。使用此代码天数不会计算?
猜你喜欢
  • 2018-04-11
  • 2016-10-30
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
  • 1970-01-01
  • 2023-02-03
相关资源
最近更新 更多