Javascript jQuery DatePicker 以今天为 maxDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7934252/
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
jQuery DatePicker with today as maxDate
提问by Emmanuel N
I would like to set today's date as a maxdate for jQuery datepicker in order to prevent users from picking date greater than today's date
我想将今天的日期设置为 jQuery datepicker 的 maxdate 以防止用户选择大于今天的日期
回答by Alex Peattie
$(".datepicker").datepicker({maxDate: '0'});
This will set the maxDate to +0 days from the current date (i.e. today). See:
这会将 maxDate 设置为从当前日期(即今天)算起的 +0 天。看:
回答by Smamatti
http://api.jqueryui.com/datepicker/#option-maxDate
http://api.jqueryui.com/datepicker/#option-maxDate
$( ".selector" ).datepicker( "option", "maxDate", '+0m +0w' );
回答by Dwight Scott
If you're using bootstrap 3 date time picker, try this:
如果您使用的是 bootstrap 3 日期时间选择器,请尝试以下操作:
$('.selector').datetimepicker({ maxDate: $.now() });
回答by tushar wason
For those who dont want to use datepicker method
对于那些不想使用日期选择器方法的人
var alldatepicker= $("[class$=hasDatepicker]");
alldatepicker.each(function(){
var value=$(this).val();
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yyyy;
if(value!=''){
if(value>today){
alert("Date cannot be greater than current date");
}
}
});