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
This question is related to
javascript
jquery
jquery-ui-datepicker
If you're using bootstrap 3 date time picker, try this:
$('.selector').datetimepicker({ maxDate: $.now() });
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");
}
}
});
In recent version, The following works fine:
$('.selector').datetimepicker({
maxDate: new Date()
});
maxDate
accepts a Date object as parameter.
The following found in documentation:
Multiple types supported:
Date: A date object containing the minimum date.
Number: A number of days from today. For example 2 represents two days from today and -1 represents yesterday.
String: A string in the format defined by the dateFormat option, or a relative date. Relative dates must contain value and period pairs; valid periods are "y" for years, "m" for months, "w" for weeks, and "d" for days. For example, "+1m +7d" represents one month and seven days from today.
http://api.jqueryui.com/datepicker/#option-maxDate
$( ".selector" ).datepicker( "option", "maxDate", '+0m +0w' );
Source: Stackoverflow.com