Hybrid View
-
31 Mar 2008 5:59 AM #1
[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:
orCode:Date.parseDate('2-08', 'n-y')
or anything else, i'll get the 2nd of March instead of 29 of February.Code:Date.parseDate('FEB-08', 'M-y')
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.
-
31 Mar 2008 7:01 AM #2
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:
you will get the wrong date, which the month was added by 1.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');
Why?
-
31 Mar 2008 7:30 AM #3
That's not a bug.
From the docs for the Date.parseDate() method (emphasis added):
note the portion in bold.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. ... ...
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):
[edit 2]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
@cjqcjq2008, in your case, since no day was specified, your current day (i.e. 31) will be used, giving the equivalent of
all of which obviously do not exist, hence the automatic "rollover" effect.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
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
31 Mar 2008 7:45 AM #4
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.
-
31 Mar 2008 7:47 AM #5
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
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
31 Mar 2008 12:58 PM #6
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?
-
31 Mar 2008 10:29 AM #7
You might want to have a quick look at Datejs.
With Datejs, your original examples would be written as...
Hope this helps.Code:Date.parseExact("2-08", "M-yy") Date.parseExact("FEB-08", "MMM-yy")
-
31 Mar 2008 10:33 AM #8Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
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); };
-
31 Mar 2008 6:00 PM #9
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
Sencha Docs / Ext 3.x - ( Docs | Examples )
Learning Center / Saki's Examples (for 2.x) / HOWTO - ( Report Bugs | Post Proper Code )
-
31 Mar 2008 7:33 AM #10Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- The Netherlands
- Posts
- 24,251
- Vote Rating
- 40
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.


Reply With Quote