Javascript 如何在日期选择器中禁用未来日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42128851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable future dates in datepicker?
提问by Praddyumna Sangvikar
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, '');
}
});
});
回答by Sorangwala Abbasali
You can do this: Datepicker has an option maxDate
您可以这样做:Datepicker 有一个选项 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>
回答by Shree
using maxDate:'0'you can acchive this.
使用maxDate:'0'你可以做到这一点。
$("#datepicker").datepicker({
dateFormat: 'yy-mm-dd ',
maxDate:'0'
});
回答by abhishek kumar
If you want to future date disable from today use today.getFullYear() - 10in order to disable 10 years before which means disable from 2008.
如果您想从今天开始禁用未来日期,请使用today.getFullYear() - 10以禁用 10 年前,这意味着从 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>
回答by ASWATH DAS O
$(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>
回答by Pergin Sheni
Simply can do by this :It will disable from today date.
只需这样做:它将从今天起禁用。
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
maxDate: "-1d",
minDate: "-100Y",
yearRange: "-100:-0"
});

