简体   繁体   English

如何使用Javascript为HTML输入日期设置范围

[英]How to set range with Javascript for HTML input date

I need to set date range for my 2 input dates. 我需要为2个输入日期设置日期范围。 I know that I can put min and max value for it but it will force me to pick a specific time range. 我知道我可以为此设置minmax ,但这会迫使我选择特定的时间范围。 What I want to achieve is to select any date in the first input and only a day within a 14 days range in the second input. 我要实现的是在第一个输入中选择任何日期,而在第二个输入中仅选择14天范围内的一天。

So for example I want to be able to pick 14.01.2014 - 28.01.2014 as well as 01.05.1992 - 15.05.1992 . 因此,例如,我希望能挑到14.01.2014 - 28.01.2014 ,以及01.05.1992 - 15.05.1992 Therefore i can't just use 因此,我不能只使用

<input type="date"  />
<input type="date"  max="{{ctrl.maxDate"}}/>

Do someone know how to dynamically set max value or is their a range attribute? 有人知道如何动态设置最大值还是他们的range属性?

You can set up maxDate to be a calculated value. 您可以将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> 

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

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