How to disable future dates in datepicker?
65,469
Solution 1
You can do this: Datepicker has an option maxDate
$(document).ready(function () {
var today = new Date();
$('.datepicker').datepicker({
format: 'mm-dd-yyyy',
autoclose:true,
endDate: "today",
maxDate: today
}).on('changeDate', function (ev) {
$(this).datepicker('hide');
});
$('.datepicker').keyup(function () {
if (this.value.match(/[^0-9]/g)) {
this.value = this.value.replace(/[^0-9^-]/g, '');
}
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Select Date: <input type="text" class="datepicker"></p>
Solution 2
using maxDate:'0'
you can acchive this.
$("#datepicker").datepicker({
dateFormat: 'yy-mm-dd ',
maxDate:'0'
});
Solution 3
If you want to future date disable from today use today.getFullYear() - 10
in order to disable 10 years before which means disable from 2008.
<script>
$(document).ready(function(){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear()-10; // change according to year 0 for current
var today1 = mm + '/' + dd + '/' + yyyy;
$("#birthday").datepicker({
endDate:today1,
});
});
</script>
Author by
Praddyumna Sangvikar
Updated on September 18, 2020Comments
-
Praddyumna Sangvikar over 3 years
I have used following code for this but it is not working. please help me to do it.
$(document).ready(function () { $('.datepicker').datepicker({ format: 'mm-dd-yyyy', autoclose:true, endDate: "today", }).on('changeDate', function (ev) { $(this).datepicker('hide'); }); $('.datepicker').keyup(function () { if (this.value.match(/[^0-9]/g)) { this.value = this.value.replace(/[^0-9^-]/g, ''); } }); });
-
Soni Vimalkumar about 7 years$ is not defined...Please add jQuery link above to complete example.
-
Soni Vimalkumar about 7 yearsPlease, reconfirm my last update. Now is finally working. :)
-
Soni Vimalkumar about 7 yearsThanks @abbasali.. :)
-
Praddyumna Sangvikar about 7 yearsI am using Bootstrap datepicker so it is not working properly
-
Sorangwala Abbasali about 7 years@PraddyumnaSangvikar what problem are you facing now?
-
abhishek kumar almost 6 yearsif you want to future date disable from today use today.getFullYear(); because -10 show disable 10 year before which means disable from 2008;
-
L. Guthardt almost 6 yearsIn the future please add additional information directly in your answer by clicking the
edit
button below your answer. -
Carlos Cavero about 5 yearsFirst of all, welcome to StackOverflow. Following the advices of the community, it is recommended to include context to your source code in order to receive the proper responses. Please check the documentation on how to write a good question
-
Zoe stands with Ukraine about 5 years@CarlosCavero You know this is an answer and not a question, right?
-
Zoe stands with Ukraine about 5 yearsEither way, take a look at how to answer
-
Carlos Cavero about 5 yearsThanks @Zoe I got confused maybe editing another question :(. My apologies