AngularJS datepicker添加到天数

时间:2017-10-18 14:18:04

标签: angularjs datepicker

我有一张AngularJS表单,其中包含“开始日期”和“#39;和#'持续时间'字段,我需要计算一个结束日期'根据用户输入这两个字段的内容,即 结束日期=开始日期+天数

但我正在努力将它们转换为正确的数字格式以计算结束日期'正确。

这是基本代码: HTML

<div ng-controller="MyCtrl">
  <p>Start Date: <md-datepicker ng-model="date" md-placeholder="Enter date" >
  </md-datepicker></p>
<p>No of days:<input ng-model="noDays" type="number"></p>
<p>End Date: {{date+ noDays}}</p>
</div>

JS

var app = angular.module('myApp', ['ngMaterial', 'ngAnimate']);
app.controller('MyCtrl', function($scope) {
  $scope.date = new Date();
});

并在这里jsfiddle https://jsfiddle.net/benct/n6jwj61s/

任何帮助表示赞赏。感谢

1 个答案:

答案 0 :(得分:1)

请参阅此处this回答

只需编写一个updateDate函数即可添加日期:

<div ng-controller="MyCtrl">
  <p>Start Date: <md-datepicker ng-model="date" md-placeholder="Enter date" ng-change='updateDate()'>
  </md-datepicker></p>
<p>No of days:<input ng-model="noDays" type="number" 
                ng-model-options='{ debounce: 1000 }'
                ng-change='updateDate()'></p>
<p>End Date: {{enddate}}</p>
</div>

JS

$scope.date = new Date();
$scope.enddate = new Date();
$scope.noDays = 0;
$scope.updateDate = function() {
   $scope.enddate.setDate($scope.date.getDate() + $scope.noDays);
}

Fiddle