问题
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