96

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
  • 54,075
  • 125
  • 354
  • 529

16 Answers16

125

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/

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • 2
    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. – Harsha M V Dec 02 '11 at 12:55
  • 1
    @HarshaMV i updated my answer, you should just set minDate when you initialize the datepickers – Nicola Peluchetti Dec 02 '11 at 12:58
  • You can add `showAnim:"",` to datepicker settings to remove annoying month and year changing when You make Your selection. – Misiu Oct 10 '12 at 12:12
  • I used your script, but I am getting error "Uncaught ReferenceError: $ is not defined". how will I fix this? – mable george Oct 14 '15 at 07:21
  • @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 – wolfQueen Feb 01 '17 at 07:10
  • @wolfQueen it depend a little on your markup, in theory it should work asp – Nicola Peluchetti Feb 01 '17 at 19:02
83

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..

T30
  • 11,422
  • 7
  • 53
  • 57
Sachin
  • 831
  • 6
  • 3
51
$('#datepicker-dep').datepicker({
    minDate: 0
});

minDate:0 works for me.

ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
Riya Travel
  • 727
  • 7
  • 8
23

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

John Hascall
  • 9,176
  • 6
  • 48
  • 72
Mosin
  • 600
  • 6
  • 18
8

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.

PålOliver
  • 2,502
  • 1
  • 23
  • 27
7

Live Demo ,try this,

    $('#from').datepicker(
     { 
        minDate: 0,
        beforeShow: function() {
        $(this).datepicker('option', 'maxDate', $('#to').val());
      }
   });
  $('#to').datepicker(
     {
        defaultDate: "+1w",
        beforeShow: function() {
        $(this).datepicker('option', 'minDate', $('#from').val());
        if ($('#from').val() === '') $(this).datepicker('option', 'minDate', 0);                             
     }
   });
hari
  • 1,874
  • 1
  • 16
  • 10
7

This is easy way to do this

$('#datepicker').datepicker('setStartDate', new Date());

also we can disable future days

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

By setting startDate: new Date()

$('.date-picker').datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 1,
    ...
    startDate: new Date(),
});
kheengz
  • 840
  • 12
  • 10
4

set startDate attribute of datepicker, it works, below is the working code

$(function(){
$('#datepicker').datepicker({
    startDate: '-0m'
    //endDate: '+2d'
}).on('changeDate', function(ev){
    $('#sDate1').text($('#datepicker').data('date'));
    $('#datepicker').datepicker('hide');
});
})
Anna
  • 1,669
  • 7
  • 38
  • 63
3

jQuery API documentation - datepicker

The minimum selectable date. When set to null, there is no minimum.

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.

In order not to display previous dates other than today

$('#datepicker').datepicker({minDate: 0});
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
3

just replace your code:

old code:

$("#dateSelect").datepicker({
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd'

});

new code:

$("#dateSelect").datepicker({
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd',
    minDate: 0
});
Tunaki
  • 132,869
  • 46
  • 340
  • 423
2

"mindate" attribute should be used to disable passed dates in jquery datepicker.

minDate: new Date() Or minDate: '0' is the key for this.

Ex:

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

OR

$(function() {
  $( "#datepicker" ).datepicker({minDate: 0});
});
gsamaras
  • 71,951
  • 46
  • 188
  • 305
sreekanth
  • 517
  • 4
  • 4
2

you can simply use

startDate: 'today'

it working fine for me.

Moaz Ateeq
  • 397
  • 1
  • 11
  • 23
1

I have created function to disable previous date, disable flexible weekend days (Like Saturday, Sunday)

We are using beforeShowDay method of jQuery UI datepicker plugin.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var NotBeforeToday = function(date) {
  var now = new Date(); //this gets the current date and time
  if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() >= now.getDate() && (date.getDay() > 0 && date.getDay() < 6) )
   return [true,""];
  if (date.getFullYear() >= now.getFullYear() && date.getMonth() > now.getMonth() && (date.getDay() > 0 && date.getDay() < 6))
   return [true,""];
  if (date.getFullYear() > now.getFullYear() && (date.getDay() > 0 && date.getDay() < 6))
   return [true,""];
  return [false,""];
 }


jQuery("#datepicker").datepicker({
  beforeShowDay: NotBeforeToday
    });

enter image description here

Here today's date is 15th Sept. I have disabled Saturday and Sunday.

Smit Patadiya
  • 1,488
  • 1
  • 9
  • 6
0

you have to declare current date into variables like this

 $(function() {
    var date = new Date();
    var currentMonth = date.getMonth();
    var currentDate = date.getDate();
    var currentYear = date.getFullYear();
    $('#datepicker').datepicker({
    minDate: new Date(currentYear, currentMonth, currentDate)
    });
})
hoga98
  • 1
0
$( "#date" ).datetimepicker({startDate:new Date()}).datetimepicker('update', new Date());

new Date() : function get the todays date previous date are locked. 100% working

Jens
  • 67,715
  • 15
  • 98
  • 113