1

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

2
  • 1
    Would you create a fiddle for this please.. Commented May 27, 2016 at 10:12
  • can't you just set min-date to the current date? Commented May 27, 2016 at 10:17

2 Answers 2

3

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;
       }
    }     
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect.
0

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; 
  })
}

3 Comments

Doing this way disables the current day after the noon. 😕
You can, of course, use any date/time as the value for today above - use what ever value you need for your situation. You might try var today = moment.utc().valueOf();
No, even this does not work , it keeps on disabling the current date too even after using const today = moment.utc().valueOf();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.