简体   繁体   English

如何在angularjs中为现有的ng-module date增加天数?

[英]How to add number of days to existing ng-module date in angularjs?

Hi all I'm new to angularjs actually i'm trying to add one ng-module value into another ng-moudule date input in angularjs... For exmaple:- 17-08-2016 add 20 Result Expecting 03-09-2016. 大家好,我是angularjs的新手,我实际上是想将一个ng-module值添加到angularjs的另一个ng-moudule date input中...例如:-17-08-2016添加20结果期望2016年3月9日。 my plunk 我的朋克

i don't know where i did the mistake and how to get the solution, please if anyone knows the answer help me for the same thanks. 我不知道我在哪里犯了错误以及如何获得解决方案,如果有人知道答案,请同样谢谢我。

My controller 我的控制器

 $scope.name = {
"_id": "57ac1b6d82e1c5940ac3c730",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"terms": "20",
"invoice_date": "2016-08-17",
"due_date": ""
};

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + parseInt(terms));
    return this;
};

My Html:- 我的HTML:

 <tr ng-repeat="mani in name"> </tr>
      <td >{{name.invoice_date | date:'dd-MM-yyyy'}}</td>
          <td >{{name.terms }}</td>
             <td >{{name.invoice_date | date:'dd-MM-yyyy'}} + {{name.terms }}</td>

You can fix it by doing this: 您可以通过执行以下操作来修复它:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.name = {
"_id": "57ac1b6d82e1c5940ac3c730",
"user": {
"_id": "57400c32bd07906c1308e2cf",
"displayName": "mani selvam"
},
"__v": 0,
"terms": "20",
"invoice_date": "2016-08-17",
"due_date": ""
};

$scope.addDays = function(stringDate,days) {
   var date = new Date(stringDate);
   date.setDate(date.getDate() + parseInt(days));
  return date;
}

});

then in your template do: 然后在您的模板中执行以下操作:

 <td >{{addDays(name.invoice_date,name.terms) | date:'dd-MM-yyyy'}}</td>

the reason why something like this doesn't work in your example: 在您的示例中无法执行此类操作的原因:

<td >{{(name.invoice_date | date:'dd-MM-yyyy').addDays({{name.terms}})}} + {{name.terms }}</td>

is because the angular date filter returns a String representation and thus is not linked to the date prototype 是因为角度日期过滤器返回String表示形式,因此未链接到日期原型

in theory this could work if your name.invoice_date would be a date object instead of a string in your json. 从理论上讲,如果您的name.invoice_date是日期对象而不是json中的字符串,则可以使用。 take a look at this from the angular filter source code 从角度滤镜源代码看一下

@param {(Date|number|string)} date Date to format either as Date object, milliseconds (string or
 *    number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.sssZ and its
 *    shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is
 *    specified in the string input, the time is considered to be in the local timezone.

so if it would be a date to begin with you could do: 因此,如果这是一个开始的日期,您可以这样做:

<td >{{(name.invoice_date).addDays({{name.terms}}) | date:'dd-MM-yyyy'}} + {{name.terms }}</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM