Skip to content Skip to sidebar Skip to footer

Future Dates Raise Error Jquery Ui Datepicker

I have that script for jquery ui datepicker var date = new Date(new Date().setDate(new Date().getDate() - 1)); $('.date').datepicker({ dateFormat: 'dd-mm-yy', defaultDate

Solution 1:

I have not tried it myself, but it may solve this problem.

change above date format like this

dateFormat:'mm-dd-yy',

or simply remove that line because default dateFormat is mm/dd/yy

Solution 2:

Date.parse() returns the number of milliseconds since January 1, 1970 for a date string, and minDate and maxDate can accept a number, but it is used in this way:

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

Therefore, the validation is expecting you to enter a date that is between 2208988800000 days in the past and 4102444800000 days in the future. These dates are incredibly distant, and are too large for a Date to handle.

To fix it, use new Date() instead.

$('.date').datepicker({
    dateFormat: 'dd-mm-yy',
    defaultDate: -1,
    minDate: newDate("1900-01-01"),
    maxDate: newDate("2100-01-01")
});

JSFiddle

Solution 3:

As I get unobtrusive validation needs to be overloaded for different format this answer helped me.

$.validator.addMethod('date',
function (value, element) {
    if (this.optional(element)) {
        returntrue;
    }

    var ok = true;
    try {
        $.datepicker.parseDate('dd-mm-yy', value);
    }
    catch (err) {
        ok = false;
    }
    return ok;
});

Post a Comment for "Future Dates Raise Error Jquery Ui Datepicker"