san
san

Reputation: 237

Restrict bootstrap datepicker to select date upto 2 years - not working

I want to restrict the date which the user can select to 2 years from today’s date, by default user can select infinite date

Following is the code I have tried

Libraries used:

ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js

maxcdn.bootstrapcdn.com/bootstrap/3.3.6

css/bootstrap.min.css css/datepicker.css

maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js

js/bootstrap-datepicker.js

<input type="text" id="datepicker1">

<script>
$('#datepicker1').datepicker({
   endDate:'+2y'
});
</script>

The above code doesn't limit the date

Please help!!!

I have also tried the following code which is working but the datepicker in this case is an inline datepicker, I do not need an inline datepicker

<head>
http://192.168.0.27:8080/hostbook/public_html/js/jquery.js

http://192.168.0.27:8080/hostbook/public_html/assests/css/bootstrap-datepicker3.css

http://192.168.0.27:8080/hostbook/public_html/assests/js/bootstrap-datepicker.js
</head>

<input name="check_in" id="check_in" type="text" class="datepicker search-date"  autocomplete="off" />
               
<script>

$(document).ready(function(){

$('#check_in').datepicker({ format: "dd/mm/yyyy",
startView: "months",
endDate:'+2y',
})

});

</script>

Upvotes: 2

Views: 5242

Answers (1)

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29693

You haven't specified startDate for datePicker to decide endDate. Just add it in option as below:

$('#datepicker1').datepicker({
   startDate:new Date(), //Today's Date
   endDate:'+2y'
});

DEMO HERE

Upvotes: 3

Related Questions