Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Datepicker getDate returns today's date on invalid date

When using jQuery UI's date-picker, if you call getDate while the text in the text box is not a valid date, getDate returns today's date.

Example

How can I distinguish between today's date and an invalid date when retrieving the date?

like image 745
Sam Avatar asked Jan 20 '26 04:01

Sam


1 Answers

Looks like this is normal behaviour for the widget. Here's a function that includes support for invalid date checking:

/* Gets the current value
 * @return Date The result or null if no date is present
 * @throws If the entered value is invalid
 */
function getDate(datePicker) {
    datePicker = $(datePicker);

    var format = datePicker.datepicker("option", "dateFormat"),
        text = datePicker.val(),
        settings = datePicker.datepicker("option", "settings");

    return $.datepicker.parseDate(format, text, settings);
}
like image 181
Sam Avatar answered Jan 22 '26 17:01

Sam