Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datepicker issue with one digit month no leading zero

I have a date format that is a one digit month (no leading zero), followed by a two digit day and 2 digit year.

January 1st, 2013 = 10113

I'm trying to attach a datepicker with the correct format to it but it doesn't format correctly.

http://jsfiddle.net/S3KyX/1/

<input type="text" id="date" value="10113">
$("#date").datepicker({ dateFormat: 'mddy'});

When the date picker pops up it has the date Oct 11, 2003.

like image 410
wkm Avatar asked Dec 05 '25 06:12

wkm


1 Answers

Unfortunately, your format won't work. if you look at the implementation of datepicker formatDate, you can see that for m it will try to match 1 or 2 digits, because it can't tell that you mean January and not October ahead of time,

This method is being called like this, getNumber('m'):

var getNumber = function(match) {
    var isDoubled = lookAhead(match);
    var size = (match == '@' ? 14 : (match == '!' ? 20 :
            (match == 'y' && isDoubled ? 4 : (match == 'o' ? 3 : 2))));
    var digits = new RegExp('^\\d{1,' + size + '}');
    var num = value.substring(iValue).match(digits);
    if (!num)
        throw 'Missing number at position ' + iValue;
    iValue += num[0].length;
    return parseInt(num[0], 10);
};

as you can see size will be 2 for 'm', therefore it will match 2 digits! When in doubt put break points in the code and see what happens (that's what I just did, using chrome developer tools)

like image 174
Bassam Mehanni Avatar answered Dec 08 '25 18:12

Bassam Mehanni