43

I want to disable all the future dates after today in Jquery Ui Datepicker

Here is the Demo :

Code :

$( "#start_date" ).datepicker(

        { 
            maxDate: '0', 
            beforeShow : function()
            {
                jQuery( this ).datepicker('option','maxDate', jQuery('#end_date').val() );
            },
            altFormat: "dd/mm/yy", 
            dateFormat: 'dd/mm/yy'

        }

);

$( "#end_date" ).datepicker( 

        {
            maxDate: '0', 
            beforeShow : function()
            {
                jQuery( this ).datepicker('option','minDate', jQuery('#start_date').val() );
            } , 
            altFormat: "dd/mm/yy", 
            dateFormat: 'dd/mm/yy'

        }

);
1

9 Answers 9

97

Try this

 $(function() {
  $( "#datepicker" ).datepicker({  maxDate: new Date() });
 });

Or you can achieve this using as below:

$(function() {
  $( "#datepicker" ).datepicker({  maxDate: 0 });
});

Reference

DEMO

UPDATED ANSWER

Sign up to request clarification or add additional context in comments.

3 Comments

@Amit: Thanks for the answer, it is working as a charm.
Somehow this $('#birth_date').datetimepicker({ timepicker:false, format:'d/m/Y', maxDate : '0', maxDateTime:new Date() }); doesn't work.
$(function() { $( "#datepicker" ).datepicker({ maxDate: new Date() }); }); is working for me
23

This worked for me endDate: "today"

  $('#datepicker').datepicker({
        format: "dd/mm/yyyy",
        autoclose: true,
        orientation: "top",
        endDate: "today"

  });

SOURCE

3 Comments

This endDate: "today" helped me
@Dnyaneshwar Harer hats off. Thank You
As of today, I had to do maxDate: "today", not endDate
9

In my case, I have given this attribute to the input tag

data-date-start-date="0d" data-date-end-date="0d"

1 Comment

This should be preferred if you have a generic JS code for all datepickers and out of those you need to restrict date in one or two fields.
7

You can simply do this

$(function() {
    $( "#datepicker" ).datepicker({  maxDate: new Date });
  });

JSFiddle

FYI: while checking the documentation, found that it also accepts numeric values too.

Number: A number of days from today. For example 2 represents two days from today and -1 represents yesterday.

so 0 represents today. Therefore you can do this too

 $( "#datepicker" ).datepicker({  maxDate: 0 });

3 Comments

Please modify my fiddle
@CodeHunter may I know for which one start_date or end_date?
For start date its obvious
3

Change maxDate to current date

maxDate: new Date()

It will set current date as maximum value.

Comments

2

In case you are appending Dtpicker,use the following code

$('#enddate').appendDtpicker({
    "dateOnly": true,
    "dateFormat": "YYYY-MM-DD",
    "closeOnSelected": true,
    maxDate: new Date()         
});

Comments

2

Datepicker does not have a maxDate as an option. I used this endDate option. It worked well.

$('.demo-calendar-default').datepicker({
  autoHide: true,
  zIndex: 2048,
  format: 'dd/mm/yyyy',
  endDate: new Date()
});

Comments

1

//Disable future dates after current date

$("#datepicker").datepicker('setEndDate', new Date());

//Disable past dates after current date

$("#datepicker").datepicker('setEndDate', new Date());

1 Comment

your code to disable past dates doesnt look right.. shouldn't it be start date?
-4
maxDate: new Date() 

its working fine for me disable with current date in date range picker

1 Comment

I don't think this adds anything to the existing answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.