javascript - AngularUI Datepicker 禁用超出范围的日期

标签 javascript angularjs date datepicker angularjs-controller

我想将 Angular UI 日期选择器限制在作为变量传入的两个日期之间。我最好在不添加像 momentjs 这样的库的情况下让它工作,因为这是我需要处理日期的唯一领域。

这里是这个问题的一个plunker:

http://plnkr.co/edit/zsjpoVZtHqJLIP2RW6vm?p=preview

这里是变量:

mycurrentdate = '2016-04-18'
mymindate = '2016-04-01'
mymaxmonth = '2016-05-01'
mymaxdate will be calculated from mymaxmonth to be
mymaxdate = '2016-05-31'

我的实际最大日期将是 mymaxmonth 的最后一天

$scope.maxDate = new Date(
                    $scope.mymaxmonth + (TO THE END OF THE MONTH)
                );

需要注意的一件事是,通过 new Date() 运行它会返回给定日期前一天的日期。例如:

$scope.minDate = new Date(
                    $scope.mymindate
                );

$scope.minDate 返回 Wed Mar 30 2016 17:00:00 GMT-0700 (PDT) 我查找了它返回 3 月 30 日而不是 4 月 1 日的原因,它看起来像是时区错误?

我想将 mymindate 设置为 '2016-04-01' 并获取 mymaxdate = '2016-05-31' 并禁用此范围之外的所有日期。我读过 Beginners Guide to Javascript Date and Time并在这里试用。

在我的 Controller 中:

$scope.mymindate = '2016-04-01';
$scope.mymaxmonth = '2016-05-01'; //want mymaxdate to be '2016-05-31'

 $scope.minDate = new Date($scope.dt.getFullYear(), $scope.dt.getMonth(), 1);

 $scope.maxDate = new Date($scope.dt.getFullYear(), $scope.dt.getMonth() + 1, 0);

在我的模板中:

    <p class="input-group">
      <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" min-date="minDate" max-date="maxDate" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" />
      <span class="input-group-btn">
        <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
      </span>
    </p>

最佳答案

您需要为您的输入设置带有适当选项datepicker-options来禁用日期。在您的示例中使用了 datepicker-options="dateOptions" 但在您的 Controller 中没有声明 dateOptions

因此您需要为maxDateminDate 设置dateOptions。喜欢

$scope.dateOptions = {
    maxDate: new Date($scope.maxDate),
    minDate: new Date($scope.mymindate)
};

并设置 maxDateminDate 如下:

$scope.mymindate = new Date('2016-04-01');
$scope.mymaxmonth = new Date('2016-05-01'); //wanted mymaxdate to be '2016-05-31'

$scope.minDate = new Date($scope.mymindate);

$scope.maxDate = new Date($scope.mymaxmonth.getFullYear(),$scope.mymaxmonth.getMonth()+1,0);

和 HTML:

<p class="input-group">
    <input  type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" min="minDate" max="maxDate" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" />
    <span class="input-group-btn">
       <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
    </span>
</p>

可以看到Plunker Demo希望它能帮到你 :)

关于javascript - AngularUI Datepicker 禁用超出范围的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36699224/

相关文章:

javascript - 如果使用 :nth-child 超过 3 个,则每个相同父 div 的 jQuery 切换嵌套 div

python - 如何在pandas中按d-month-y格式的日期排序?

javascript - Angular 和 ParseSDK Javascript CRUD 操作

ruby - 为什么给时间加上一天会改变秒的小数部分?

java - 使用具有 long 值的 java.sql.Date 构造函数

JavaScript removeChild(this) from input[type ="submit"] onclick 破坏了 Firefox 下 form.submit() 的 future 使用

javascript - 输入状态在第一次单击时未绑定(bind) onChange

javascript - 如何根据点击获取对应对象的值

javascript - 在 Node.js 项目中包含 .css 和 .js 文件

javascript - 使用 Angular js 从 json 数组中删除重复值