7

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.

jonathan.cone
  • 6,592
  • 2
  • 30
  • 30
  • 1
    Why won't the jQueryUI Datepicker dateFormat work for you? http://jqueryui.com/demos/datepicker/#date-formats – James Kovacs Dec 03 '10 at 16:21
  • 1
    I want the user to be able to enter the date in a variety of formats, I don't really care which one and I don't want them to have to specify in advance. dateFormat only accepts one format. – jonathan.cone Dec 03 '10 at 16:36

5 Answers5

22

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;
};
cage rattler
  • 1,587
  • 10
  • 16
  • 1
    Does the line `testParse(format, value, settings);` mean that the original dateFormat is tested first and then all of `inputFormats`? If so, `inputFormats` should be called `additionalFormats`? – mnsc Feb 05 '14 at 19:50
  • can you also please add the usage. And should I add your code inside jquery ui file and modify it? thanks – user786 Jun 04 '15 at 10:25
  • Sorry to take so long to respond to your question, Alex, but if anyone is still considering that decision, I wouldn't modify your library source file except as a last resort. When a method is exposed, as this one is, just overwrite it in your own code. – cage rattler May 17 '16 at 13:52
  • Wrote an updated answer based on the remarks, see below. – LePatay Nov 27 '18 at 12:19
  • 1
    The above solution breaks datePicker.setDate() for relative date strings ( -1y, +2w, etc ). datePicker.setDate() expects an Error to be thrown out of the parseDate() method when parsing relative dates. Adding a Throw at the end of this function if the date can't be parsed restores that functionality. ( repeat comment from below, but added here as this is the highest rated answer ) – Alex Colomb May 01 '23 at 19:18
1

You can attempt to interpret multiple input formats and replace the characters on the blur event:

$("#datepicker").datepicker({
    constrainInput: false
}).blur(function() {
    var txt = $(this).val();
    $(this).val(txt.replace(/-| /g, '/'));
});

Unfortunately, if I cannot find a way to do the same if the Enter key is pressed.

tinifni
  • 2,372
  • 18
  • 15
0

This 'Unobtrusive Date-Picker Widget V5' seems to accept multiple formats. I did a quick test of all three of the formats you supplied, and everyone seemed to be accepted.

Demo : http://www.frequency-decoder.com/demo/date-picker-v5/

dochoffiday
  • 5,515
  • 6
  • 32
  • 41
0

Cage rattler answered that you can override the jquery datepicker function parseDate to support multiple formats. (I can't comment due to reputation, but need to add this information regardless)

I found that you also need to override the formatDate and _possibleChars functions if you are setting the format variable within the datepicker to be the list of formats. The datepicker doesn't expect a list (in my case JSON) and you have to work around this.

Also consider that if you don't override these two, they won't behave as expected. E.g. you will not be able to type the characters that are in your custom format list (except for the first format that is stored in the datepicker itself).

Liam Steele
  • 146
  • 8
0

Improvement of the solution from @cage rattler, based notably on the remark from @mnsc:

// Accepted additional formats [here, allow for example "4/7/18" for "July 4, 2018"]
$.datepicker.additionalFormats = ["ddmmy", "ddmmyy", "d/m/y", "d/m/yy", "d/mm/y", "dd/m/y", "dd/m/yy", "dd/mm/y"];
// Backup the original parsing function
$.datepicker.originalParseDate = $.datepicker.parseDate;
// New parsing function
$.datepicker.parseDate = function (format, value, settings) {
    // Function that returns a date based on an input format
    function testParse(inputFormat) {
        try {
            // Try to get the date based on the input format
            date = $.datepicker.originalParseDate(inputFormat, value, settings);
            // If the date is right, returns it immediately
            return date;
        }
        catch (Error) {
            // _RD Bad date (this line does nothing but catching the error)
        }
    }
    // Initialise the returned date
    var date;
    // Don't bother with empty values
    if (value !== "0" && value !== "") {
        // Test the main datepicker format (which is an argument of this function)
        testParse(format);
        // If the main datepicker format didn't work, try with the additional ones
        for (var n = 0; n < $.datepicker.additionalFormats.length; n++) {
            testParse($.datepicker.additionalFormats[n]);
        }
    }
    // If the input string couldn't be parsed, returns just the "undefined"-typed variable
    return date;
};

Additionally, if you want to reformat the input when leaving the field, see my answer here on this topic: https://stackoverflow.com/a/53499934/5426777

LePatay
  • 172
  • 1
  • 8
  • The above solution breaks datePicker.setDate() for relative date strings ( -1y, +2w, etc ). datePicker.setDate() expects an Error to be thrown out of the parseDate() method when parsing relative dates. Adding a Throw at the end of this function if the date can't be parsed restores that functionality. – Alex Colomb May 01 '23 at 18:24