0

I am using the Bootstrap Datepicker plugin by eyecon, found here - http://www.eyecon.ro/bootstrap-datepicker/

I am attempting to use the onRender event to disable many dates from being chosen. I need to disable all dates that are after today (future dates) for the Checkin and Checkout. The Checkout also needs any dates equal to or before the Checkin disabled too. The Checkin works perfectly, although the Checkout is not working at all.

Pretty sure the problem is me not returning the value correctly, because my logic seems fine.

Here's my attempt, using the code from the example off the Datepicker website:

HTML:

Check in: <input type="text" class="span2" value="" id="dpd1"></input> Check out: <input type="text" class="span2" value="" id="dpd2"></input> 

JS:

var nowTemp = new Date(); var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0); var checkin = $('#dpd1').datepicker({ onRender: function (date) { return date.valueOf() < now.valueOf() ? 'disabled' : ''; } }).on('changeDate', function (ev) { if (ev.date.valueOf() > checkout.date.valueOf()) { var newDate = new Date(ev.date) newDate.setDate(newDate.getDate() + 1); checkout.setValue(newDate); } checkin.hide(); $('#dpd2')[0].focus(); }).data('datepicker'); var checkout = $('#dpd2').datepicker({ onRender: function (date) { return (date.valueOf() > now.valueOf()) && (date.valueOf() <= checkin.date.valueOf()) ? 'disabled' : ''; } }).on('changeDate', function (ev) { checkout.hide(); }).data('datepicker'); 

WORKING DEMO

0

1 Answer 1

2

You need to check if the selected date is less than and not greater than the checkout date.

 if (ev.date.valueOf() < checkout.date.valueOf()) { 

And onRender method for checkout you were checking if the date is ahead of today and before checkin date which can never be true. You should check the case where it's between the checkin date and today. Hope this is what you wanted.

onRender: function (date) { return (date.valueOf() < now.valueOf()) && (date.valueOf()>= checkin.date.valueOf()) ? '' : 'disabled'; } 

Demo fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

what if you need to disable the dates from 5 days after the checkin date?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.