0

I am stuck on blocking dates in bootstrap datepicker. I need it have a range of 7 days from the starting date and blocks all the dates which is not in the 7 days

for example the user chooses 11th April 2015. That user can only select dates until 18th April 2015 which would be the ending date. Can someone please help me.

Thanks.

Here is my code for the date picker bootstrap.

var nowDate = new Date(); var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0); $('#startdate').datetimepicker({ minDate:today, format: 'YYYY-MM-DD HH:MM:SS' }).change(function (selected) { var startDate = new Date(selected.date.valueOf()); $('#enddate').datetimepicker('maxDate', startDate+10); }); 
3
  • first off, i spot you havent closed your function yet, your main function yet. indenting it shows you that ;) Commented Apr 11, 2015 at 16:36
  • Is there any particular reason why you're not setting both minDate and maxDate in the options object? Commented Apr 11, 2015 at 16:43
  • 1
    @Wex Seems that '#startdate' and '#enddate' are two different datepicker. He would like '#enddate' to dynamically update as '#startdate' changes. Commented Apr 11, 2015 at 17:09

1 Answer 1

1

First of all, you cannot directly do arithmetic operations to a Date object. Instead, you should do this.

function getEndDate(date) { date.setDate(date.getDate() + 10); return date; } 

Secondly, you should be able to set the minDate and maxDate options.

$('#startdate').datetimepicker({ minDate: today, format: 'YYYY-MM-DD HH:MM:SS' }).change(function (selected) { var startDate = new Date(selected.date.valueOf()); $('#enddate').datetimepicker('maxDate', getEndDate(startDate)); }); 
Sign up to request clarification or add additional context in comments.

2 Comments

No it still does not work. The end date calendar won't show up
@nadia Can you update the question with all the code you now have?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.