disable all previous dates in angular bootstrap datetimepicker

混江龙づ霸主 提交于 2019-12-11 11:32:53

问题


I am creating a project using angularjs. I am integrated the angulardatetimepicker from the link:

https://github.com/dalelotts/angular-bootstrap-datetimepicker.

Now I want to disable all previous dates from current date. Here is my code:

 function beforeRender($view, $dates, $leftDate, $upDate, $rightDate) {
    var minDate = moment().startOf($view).valueOf();  
    for(var i=1; i < $dates.length;i++) {
       if(new Date().getTime() > $dates[i].utcDateValue) {
          $dates[i].selectable = false;
       }
    }     
    }

This code not disabled dates correctly


回答1:


Minor Changes in your loop:

   function beforeRender($view, $dates, $leftDate, $upDate, $rightDate) {
    var minDate = moment().startOf($view).valueOf();  
    for(var i=0; i < $dates.length;i++) {
       if(minDate > $dates[i].utcDateValue) {
          $dates[i].selectable = false;
       }
    }     
    }



回答2:


Consider avoiding for...loops:

function beforeRender($view, $dates) {
  var today = moment().valueOf(); 

  $dates.filter(function (date) {
    return date.localDateValue() < today
  }).forEach(function (date) {
    date.selectable = false; 
  })
}


来源:https://stackoverflow.com/questions/37480646/disable-all-previous-dates-in-angular-bootstrap-datetimepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!