jQuery Date Picker - disable past dates

347,292

Solution 1

You must create a new date object and set it as minDate when you initialize the datepickers

<label for="from">From</label> <input type="text" id="from" name="from"/> <label for="to">to</label> <input type="text" id="to" name="to"/>

var dateToday = new Date();
var dates = $("#from, #to").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3,
    minDate: dateToday,
    onSelect: function(selectedDate) {
        var option = this.id == "from" ? "minDate" : "maxDate",
            instance = $(this).data("datepicker"),
            date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
        dates.not(this).datepicker("option", option, date);
    }
});

Edit - from your comment now it works as expected http://jsfiddle.net/nicolapeluchetti/dAyzq/1/

Solution 2

Declare dateToday variable and use Date() function to set it.. then use that variable to assign to minDate which is parameter of datepicker.

var dateToday = new Date(); 
$(function() {
    $( "#datepicker" ).datepicker({
        numberOfMonths: 3,
        showButtonPanel: true,
        minDate: dateToday
    });
});

That's it... Above answer was really helpful... keep it up guys..

Solution 3

$('#datepicker-dep').datepicker({
    minDate: 0
});

minDate:0 works for me.

Solution 4

Use the "minDate" option to restrict the earliest allowed date. The value "0" means today (0 days from today):

    $(document).ready(function () {
        $("#txtdate").datepicker({
            minDate: 0,
            // ...
        });
    });

Docs here: http://api.jqueryui.com/datepicker/#option-minDate

Solution 5

Just to add to this:

If you also need to prevent the user to manually type a date in the past, this is a possible solution. This is what we ended up doing (based on @Nicola Peluchetti's answer)

var dateToday = new Date();

$("#myDatePickerInput").change(function () {
    var updatedDate = $(this).val();
    var instance = $(this).data("datepicker");
    var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, updatedDate, instance.settings);

    if (date < dateToday) {
        $(this).datepicker("setDate", dateToday);
    }
});

What this does is to change the value to today's date if the user manually types a date in the past.

Share:
347,292
Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on September 03, 2020

Comments

  • Harsha M V
    Harsha M V almost 4 years

    I am trying to have a date Range select using the UI date picker.

    in the from/to field people should not be able to view or select dates previous to the present day.

    This is my code:

    $(function() {
        var dates = $( "#from, #to" ).datepicker({
            defaultDate: "+1w",
            changeMonth: true,
            numberOfMonths: 1,
            onSelect: function( selectedDate ) {
                var option = this.id == "from" ? "minDate" : "maxDate",
                    instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
                dates.not( this ).datepicker( "option", option, date );
            }
        });
    });
    

    Can some one tell me how to disable dates previous the to the present date.

  • Harsha M V
    Harsha M V over 12 years
    i want to implement a way that people should not be able to pick a date which has been past.. like yesterday day before etc. Only from today and onwards.
  • Nicola Peluchetti
    Nicola Peluchetti over 12 years
    @HarshaMV i updated my answer, you should just set minDate when you initialize the datepickers
  • Misiu
    Misiu over 11 years
    You can add showAnim:"", to datepicker settings to remove annoying month and year changing when You make Your selection.
  • mable george
    mable george over 8 years
    I used your script, but I am getting error "Uncaught ReferenceError: $ is not defined". how will I fix this?
  • wolfQueen
    wolfQueen over 7 years
    @NicolaPeluchetti This is exactly what I need, but can this be used in asp:Textbox? I tried but its not working, is there any specific change that I should do? Thank you in advance
  • Nicola Peluchetti
    Nicola Peluchetti over 7 years
    @wolfQueen it depend a little on your markup, in theory it should work asp
  • Ansif
    Ansif over 2 years
    This works for me. Thanks