Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datepicker that supports multiple formats

I'm wondering if anyone can suggest a plugin or solution that would allow me to use the jQuery datepicker plugin with multiple input date formats at the same time. This would allow the user to enter the date in any of the specified formats. I.e.:

3 31 10
3/31/2010
3-31-10

I don't really care if the value gets mangled to one of the specific formats after the user tabs out. A good input masking plugin might work for my purpose too, however, this popular one seems to expect a fixed cardinality for each of the fields, which won't work because I want the user to be able to enter 3 or 03 for the month of March, for example.

like image 835
jonathan.cone Avatar asked Dec 03 '10 16:12

jonathan.cone


1 Answers

It's been a while since this question was active, but if anyone is interested in solving the problem using the simpler jQueryUI datepicker, it can be accomplished by tweaking its parseDate method:

$.datepicker.inputFormats = ["dd-mm-yy", "dd/mm/yy", "dd mm yy"];//list formats to be accepted here

$.datepicker.originalParseDate = $.datepicker.parseDate;

$.datepicker.parseDate = function (format, value, settings) {
    var date;

    function testParse(format, value, settings) {
        if ( ! date ) {
            try {
                date = $.datepicker.originalParseDate(format, value, settings);
            } catch (Error) {
            }
        }
    }

    testParse(format, value, settings);
    for(var n = 0, stop = $.datepicker.inputFormats ? $.datepicker.inputFormats.length : 0; n < stop; n++){
        testParse($.datepicker.inputFormats[n], value, settings);
    };
    return date;
};
like image 111
cage rattler Avatar answered Sep 18 '22 11:09

cage rattler