我需要为我的 2 个输入日期设置日期范围。我知道我可以为其设置 min
和 max
值,但这会迫使我选择一个特定的时间范围。我想要实现的是在第一个输入中选择任何日期,在第二个输入中仅选择 14 天范围内的一天。
例如,我希望能够选择 14.01.2014
- 28.01.2014
以及 01.05.1992
- 1992 年 5 月 15 日
。因此我不能只使用
<input type="date" />
<input type="date" max="{{ctrl.maxDate"}}/>
有人知道如何动态设置最大值或者它们是范围属性吗?
最佳答案
您可以将 maxDate
设置为计算值。
var myApp = angular.module('myApp', []);
myApp.controller('MyController', ['$scope', function($scope) {
//Data
$scope.startDate = new Date();
$scope.range = 14;
//Functions
$scope.maxDate = function () {
var d = new Date($scope.startDate);
d.setDate($scope.startDate.getDate() + $scope.range);
return d;
};
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyController">
<input type="date" ng-model="startDate" />
<input type="date" min="{{ startDate | date:'yyyy-MM-dd' }}" max="{{ maxDate() | date:'yyyy-MM-dd' }}" />
</div>
关于javascript - 如何使用 Javascript 设置 HTML 输入日期范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32630804/