Hybrid View

  1. #1
    Ext User
    Join Date
    Aug 2007
    Posts
    4
    Vote Rating
    0
    BlackTheMad is on a distinguished road

      0  

    Default [2.0.2][CLOSED] Date.parseDate() error with no day specified

    [2.0.2][CLOSED] Date.parseDate() error with no day specified


    Hi there, creators of ExtJS.
    I've got a strange error. Today, is the 31 of March.
    And if i'll try to parse such a date:
    Code:
    Date.parseDate('2-08', 'n-y')
    or
    Code:
    Date.parseDate('FEB-08', 'M-y')
    or anything else, i'll get the 2nd of March instead of 29 of February.
    As i understood it happens because the 31st of February is not exist. So we got 2nd of March.
    To my mind it's no good.

    System:
    ExtJS 2.0.2 (ext-base.js)
    Firefox 2.0.13
    WindowsXP

    Thanks for ExtJS,
    BTheMad.

  2. #2
    Ext User
    Join Date
    Mar 2008
    Posts
    11
    Vote Rating
    0
    cjqcjq2008 is on a distinguished road

      0  

    Default i got the same error

    i got the same error


    to recur, you must set your system date to Mar,31th,2008
    try to parse such a date:
    Code:
    Date.parseDate('2008-02', 'Y-m');
    Date.parseDate('2008-04', 'Y-m');
    Date.parseDate('2008-06', 'Y-m');
    Date.parseDate('2008-09', 'Y-m');
    Date.parseDate('2008-11', 'Y-m');
    you will get the wrong date, which the month was added by 1.
    Why?

  3. #3
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    That's not a bug.

    From the docs for the Date.parseDate() method (emphasis added):
    Code:
    Date.parseDate( String input, String format ) : Date
    
    ... ... Note that this function expects dates in normal calendar format,
    meaning that months are 1-based (1 = January) and not zero-based like in JavaScript dates.
    Any part of the date format that is not specified will default to the current date value for that part. ... ...
    note the portion in bold.
    i.e. if you don't specify a day, it will default to the current day.
    (likewise, the current month will be used if none is specified etc etc)

    as to why you're observing the "rollover" date effect:
    the Ext.Date methods are simply additions to the native ecmascript (i.e. javascript) Date object. The native javascript Date object does some smart(?) internal calculations to avoid invalid dates -- this means you'll never be able to create an invalid Date object (i.e. one that holds an invalid date). The rollover you're seeing is due to these smart(?) calculations by the javascript Date object to prevent the creation of invalid Date objects.

    If you have firebug installed, fire up a plain old webpage (that doesn't have any javascript libraries loaded) and try to create an invalid Date object. I'm dead sure you won't succeed, and i won't be eating my words.

    [edit 1]
    some examples of the rollover effect (try these in FireBug on a plain old webpage):
    Code:
    new Date(2008, 0, 32); // 32nd January 2008
    new Date(2008, 1, 30); // 30th Feb 2008
    new Date(2008, 5, 60); // 60th June 2008
    [edit 2]
    @cjqcjq2008, in your case, since no day was specified, your current day (i.e. 31) will be used, giving the equivalent of
    Code:
    new Date(2008, 1,  31); // 31st Feb 2008
    new Date(2008, 3,  31); // 31st Apr 2008
    new Date(2008, 5,  31); // 31st Jun 2008
    new Date(2008, 8,  31); // 31st Sep 2008
    new Date(2008, 10, 31); // 31st Nov 2008
    all of which obviously do not exist, hence the automatic "rollover" effect.

  4. #4
    Ext User
    Join Date
    Mar 2008
    Posts
    43
    Vote Rating
    0
    rrschwartz is on a distinguished road

      0  

    Default


    There still should be a way to pass an alternate method to do date validation that will disallow the rollover. I have tried passing another validation function as an argument, and it DOES NOT override the rollover effect. Please advise.

  5. #5
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    Quote Originally Posted by rrschwartz View Post
    There still should be a way to pass an alternate method to do date validation that will disallow the rollover. I have tried passing another validation function as an argument, and it DOES NOT override the rollover effect. Please advise.
    please post your alternate date validation method + any associated code so someone can take a look at it

    [edit 1]
    this might come in handy while you're busy posting your code
    http://extjs.com/forum/showthread.php?p=76950#post76950

    [edit 2]
    or this:
    http://extjs.com/forum/showthread.php?p=76330#post76330

  6. #6
    Ext User
    Join Date
    Mar 2008
    Posts
    43
    Vote Rating
    0
    rrschwartz is on a distinguished road

      0  

    Default


    I used the Ext.override patch, and it works pretty well. However, I still need help with the following problem. Suppose I submit the form with an invalid date using a Strtus application. When the form re-displays, rather than display the incorrect values that were originally entered, the date values will come up blank. Is there anyway to display the erroneous values on the screen?

  7. #7
    Ext Partner geoffrey.mcgill's Avatar
    Join Date
    Feb 2008
    Location
    Canada
    Posts
    84
    Vote Rating
    1
    geoffrey.mcgill is on a distinguished road

      0  

    Default


    Quote Originally Posted by rrschwartz View Post
    There still should be a way to pass an alternate method to do date validation that will disallow the rollover. I have tried passing another validation function as an argument, and it DOES NOT override the rollover effect. Please advise.
    You might want to have a quick look at Datejs.

    With Datejs, your original examples would be written as...

    Code:
    Date.parseExact("2-08", "M-yy")
    Date.parseExact("FEB-08", "MMM-yy")
    Hope this helps.

  8. #8
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    94
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Or you could use the following code change:

    Code:
    Date.createParser = function(format) {
        var funcName = "parse" + Date.parseFunctions.count++;
        var regexNum = Date.parseRegexes.length;
        var currentGroup = 1;
        Date.parseFunctions[format] = funcName;
        var code = "Date." + funcName + " = function(input){\n"
            + "var y = -1, m = -1, d = -1, h = -1, i = -1, s = -1, ms = -1, o, z, u, v;\n"
            + "input = String(input);var d = new Date();\n"
            + "y = d.getFullYear();\n"
            + "m = d.getMonth();\n"
            + "d = 1;\n"
            + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
            + "if (results && results.length > 0) {";
        var regex = "";
        var special = false;
        var ch = '';
        for (var i = 0; i < format.length; ++i) {
            ch = format.charAt(i);
            if (!special && ch == "\\") {
                special = true;
            }
            else if (special) {
                special = false;
                regex += String.escape(ch);
            }
            else {
                var obj = Date.formatCodeToRegex(ch, currentGroup);
                currentGroup += obj.g;
                regex += obj.s;
                if (obj.g && obj.c) {
                    code += obj.c;
                }
            }
        }
        code += "if (u)\n"
            + "{v = new Date(u * 1000);}"
            + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0 && ms >= 0)\n"
            + "{v = new Date(y, m, d, h, i, s, ms);}\n"
            + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0)\n"
            + "{v = new Date(y, m, d, h, i, s);}\n"
            + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0)\n"
            + "{v = new Date(y, m, d, h, i);}\n"
            + "else if (y >= 0 && m >= 0 && d > 0 && h >= 0)\n"
            + "{v = new Date(y, m, d, h);}\n"
            + "else if (y >= 0 && m >= 0 && d > 0)\n"
            + "{v = new Date(y, m, d);}\n"
            + "else if (y >= 0 && m >= 0)\n"
            + "{v = new Date(y, m);}\n"
            + "else if (y >= 0)\n"
            + "{v = new Date(y);}\n"
            + "}return (v && (z || o))?\n"
            + "    (z ? v.add(Date.SECOND, (v.getTimezoneOffset() * 60) + (z*1)) :\n"
            + "        v.add(Date.HOUR, (v.getGMTOffset() / 100) + (o / -100))) : v\n"
            + ";}";
        Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i");
        eval(code);
    };

  9. #9
    Sencha - Community Support Team mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
    mystix will become famous soon enough

      0  

    Default


    Quote Originally Posted by geoffrey.mcgill View Post
    You might want to have a quick look at Datejs.

    With Datejs, your original examples would be written as...

    Code:
    Date.parseExact("2-08", "M-yy")
    Date.parseExact("FEB-08", "MMM-yy")
    Hope this helps.
    ah yes. Datejs -- almost forgot about it.
    i played with that at the start of the year, and it's indeed a very powerful js date manipulation library. plus it's lightweight -- 9kb for the core functions, and 29kb for the full package.

    one of the authors provided useful Datejs-ExtJS integration info in this thread:
    http://groups.google.com/group/datej...43b2eb7372405c

  10. #10
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    94
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    The date parser will use the current year, month and day if corresponding values are not found in the string.

    So, parsing 2008-02 on 2008-03-31 will result in 2008-02-31, which becomes 2008-03-02.

    I can't see a simple solution, because the parser also needs to support parsing time only.