0

I'm attempting to disable certain dates in the Bootstrap Date Time Picker by eonasdan found here.

I'm able to manually add an array of dates that then become disabled but I can't figure out how to add an array created dynamically into this option.

Here is my code:

noDates = ['31/03/2017', '19/04/2017', '20/04/2017', '25/04/2017'];

// Just for testing
console.log(noDates);

// Studio 1 datepicker
$('#datetimepicker1').datetimepicker({
    // Date format
    format: "DD/MM/YYYY",
    // Show calendar when user clicks input field
    allowInputToggle: true,
    // Minimum date is today's date
    minDate: new Date(),
    // Disabled Dates
    disabledDates: noDates
}); 

Where it says disabledDates: noDates I am able to manually write out the dates like so:

disabledDates: [
    "03/31/2017",
    "04/19/2017",
    moment("04/20/2017"),
    moment("04/25/2017")
]

I have also attempted the following:

$('#datetimepicker1').data('DateTimePicker').disabledDates(noDates);

However, that gives me the error:

Uncaught TypeError: Cannot read property 'disabledDates' of undefined

Is there a way to add variables/arrays into objects like this?

1 Answer 1

1

The problem is with your dates, more specific the format:

Wrong:

var noDates = ['31/03/2017', '19/04/2017', '20/04/2017', '25/04/2017'];

OK:

var noDates = ['2017-03-31', '2017-04-19', '2017-04-20', '2017-04-25'];
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, would you be able to give me a little explanation? The only difference I see with that and my code is the var but that makes no difference when I add it?
AMAZING! Thanks very much! That makes me wonder though: How is it okay to add that exact array into the datetimepicker function but I can't do it using a variable?
The disabledDates expects an array.
Okay, still slightly confused as it was an array all along, it just had the date format wrong?
Yes. That was the only problem.
|

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.