简体   繁体   中英

Setting the minimum date of jquery datepicker

I am working on a project which involves using HTML5 and Javacript/JQuery. I am trying to make use of the Jquery datepicker and trying to set the minimum date to today's date so the user can't select a date from yesterday.

Below is my Javascript.

$(function()
            {
               var today = new Date();
               $("#txtStartDate").datepicker(
                    {
                        changeMonth: true,
                        numberOfMonths: 1,
                        minDate: new Date(today.getYear(), today.getMonth() +1, today.getDay()),
                        onClose: function(selectedDate)
                        {
                            $("#txtStartDate").datepicker("option", "dateFormat", "dd-mm-yy", selectedDate);
                        }
                    });
            });

It doesn't seem to be making any difference as I can still go back to days before yesterday.

Thanks for any help you can provide.

I guess you did'nt read the documentation ?

minDate

Type : Date or Number or String

Default: null 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. <- set to 0, it's no days from today !

So setting minDate to 0 should work, right !

$(function () {
    var today = new Date();
    $("#txtStartDate").datepicker({
        changeMonth: true,
        numberOfMonths: 1,
        minDate: 0,
        onClose: function (selectedDate) {
            $("#txtStartDate").datepicker("option", "dateFormat", "dd-mm-yy", selectedDate);
        }
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM