Validate date in dd/mm/yyyy format using JQuery Validate [duplicate]

Cipher提出了一个问题:Validate date in dd/mm/yyyy format using JQuery Validate [duplicate],或许与您遇到的问题类似。

回答者izstas给出了该问题的处理方式:

You don't need the date validator. It doesn't support dd/mm/yyyy format, and that's why you are getting "Please enter a valid date" message for input like 13/01/2014. You already have the dateITA validator, which uses dd/mm/yyyy format as you need.

Just like the date validator, your code for dateGreaterThan and dateLessThan calls new Date for input string and has the same issue parsing dates. You can use a function like this to parse the date:

function parseDMY(value) {
    var date = value.split("/");
    var d = parseInt(date[0], 10),
        m = parseInt(date[1], 10),
        y = parseInt(date[2], 10);
    return new Date(y, m - 1, d);
}

希望本文对你有帮助,欢迎支持JavaScript中文网