jQuery DatePicker with today as maxDate
JavascriptJqueryJquery Ui-DatepickerJavascript Problem Overview
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
Javascript Solutions
Solution 1 - Javascript
$(".datepicker").datepicker({maxDate: '0'});
This will set the maxDate to +0 days from the current date (i.e. today). See:
Solution 2 - Javascript
http://api.jqueryui.com/datepicker/#option-maxDate
$( ".selector" ).datepicker( "option", "maxDate", '+0m +0w' );
Solution 3 - Javascript
If you're using bootstrap 3 date time picker, try this:
$('.selector').datetimepicker({ maxDate: $.now() });
Solution 4 - Javascript
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.
Solution 5 - Javascript
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");
}
}
});