parmarmayur9090 / jquery-datepicker

Automatically exported from code.google.com/p/jquery-datepicker
0 stars 0 forks source link

Selecting 29 Feb 2012 (a leap day) puts 1 March 2012 into the input box #195

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Can be seen on basic demo page:

http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePicker.html

Original issue reported on code.google.com by kelvin.l...@gmail.com on 15 Apr 2010 at 9:51

GoogleCodeExporter commented 8 years ago
have u solved it kelvin?

Original comment by fulus.in...@gmail.com on 19 Apr 2010 at 6:59

GoogleCodeExporter commented 8 years ago
Hi Kevin,

Great work on the date picker, I think I may have a fix for this issue. The 
issue is in the Date.fromString function. We need to set the returned date in 
the following order: year, month, and date to account for 29 Feb in leap years. 
Hope this helps

Tony

Date.fromString = function(s)
    {
        var f = Date.format;

        var d = new Date('01/01/1970');

        if (s == '') return d;

        s = s.toLowerCase();
        var matcher = '';
        var order = [];
        var r = /(dd?d?|mm?m?|yy?yy?)+([^(m|d|y)])?/g;
        var results;
        while ((results = r.exec(f)) != null)
        {
            switch (results[1]) {
                case 'd':
                case 'dd':
                case 'm':
                case 'mm':
                case 'yy':
                case 'yyyy':
                    matcher += '(\\d+\\d?\\d?\\d?)+';
                    order.push(results[1].substr(0, 1));
                    break;
                case 'mmm':
                    matcher += '([a-z]{3})';
                    order.push('M');
                    break;
            }
            if (results[2]) {
                matcher += results[2];
            }

        }
        var dm = new RegExp(matcher);
        var result = s.match(dm);
        var resYear, resMonth, resDate;
        for (var i=0; i<order.length; i++) {
            var res = result[i+1];
            switch(order[i]) {
                case 'd':
                    resDate = res;
                    break;
                case 'm':
                    resMonth = Number(res)-1;
                    break;
                case 'M':
                    for (var j=0; j<Date.abbrMonthNames.length; j++) {
                        if (Date.abbrMonthNames[j].toLowerCase() == res) break;
                    }
                    resMonth = j;
                    break;
                case 'y':
                    resYear = res;
                    break;
            }
        }
        // We need to set in this order to make sure 29 Feb in leap years are accounted for
        if(resYear) d.setYear(resYear);
        if(resMonth) d.setMonth(resMonth);
        if(resDate) d.setDate(resDate);
        return d;
    };

Original comment by mrwo...@gmail.com on 8 Apr 2011 at 3:44

GoogleCodeExporter commented 8 years ago
Thanks, this seemed to work for me!

Original comment by marcus.f...@gmail.com on 9 Jun 2011 at 9:21

GoogleCodeExporter commented 8 years ago
Issue 132 has been merged into this issue.

Original comment by kelvin.l...@gmail.com on 9 Jun 2011 at 9:32

GoogleCodeExporter commented 8 years ago
Issue 307 has been merged into this issue.

Original comment by kelvin.l...@gmail.com on 21 Sep 2011 at 8:09

GoogleCodeExporter commented 8 years ago
Thanks, this code works for me!

Original comment by mohammad...@kritnu.com on 3 Feb 2012 at 10:43

GoogleCodeExporter commented 8 years ago
Any chance of adding this fix to the copy held in SVN

Original comment by hubme...@gmail.com on 4 Feb 2012 at 7:06

GoogleCodeExporter commented 8 years ago
Issue 338 has been merged into this issue.

Original comment by kelvin.l...@gmail.com on 21 Feb 2012 at 9:15

GoogleCodeExporter commented 8 years ago
Thank~

Original comment by elf.engl...@gmail.com on 23 Feb 2012 at 4:53

GoogleCodeExporter commented 8 years ago
Thx! 
Works just fine!

Original comment by vincent....@gmail.com on 27 Feb 2012 at 9:16

GoogleCodeExporter commented 8 years ago
Thanks! Works for me too.

Original comment by peter.ag...@gmail.com on 28 Feb 2012 at 3:30

GoogleCodeExporter commented 8 years ago
Thank you very much. This works for me too.

Original comment by bila...@gmail.com on 8 Mar 2012 at 8:31