New issue 45469 by bart.peremans: Date.localeFormat doesn't return a value
http://code.google.com/p/chromium/issues/detail?id=45469
Chrome Version : 5.0.375.55
URLs (if applicable) : -
Other browsers tested:
Add OK or FAIL after other browsers where you have tested this issue:
Safari 4: OK
Firefox 3.x: OK
IE 7: OK
IE 8: OK
What steps will reproduce the problem?
1. javascript doesn't returns the wanted result when gettings local
formatted string from date
What is the expected result?
(new Date("6/1/2010")).localeFormat("MMMM")
returns "June"
(new Date("1/1/2010")).localeFormat("MMMM")
returns "January"
(new Date("2/1/2010")).localeFormat("MMMM")
returns "February"
(new Date("3/1/2010")).localeFormat("MMMM")
returns "March"
(new Date("4/1/2010")).localeFormat("MMMM")
returns "April"
(new Date("5/1/2010")).localeFormat("MMMM")
returns "May"
(new Date("6/1/2010")).localeFormat("MMMM")
returns "June"
(new Date("7/1/2010")).localeFormat("MMMM")
returns "July"
(new Date("8/1/2010")).localeFormat("MMMM")
returns "August"
(new Date("9/1/2010")).localeFormat("MMMM")
returns "September"
(new Date("10/1/2010")).localeFormat("MMMM")
returns "October"
(new Date("11/1/2010")).localeFormat("MMMM")
returns "November"
(new Date("12/1/2010")).localeFormat("MMMM")
returns "December"
Same issues with day names !
What happens instead?
(new Date("6/1/2010")).localeFormat("MMMM")
returns ""
(new Date("1/1/2010")).localeFormat("MMMM")
returns "January"
(new Date("2/1/2010")).localeFormat("MMMM")
returns ""
(new Date("3/1/2010")).localeFormat("MMMM")
returns ""
(new Date("4/1/2010")).localeFormat("MMMM")
returns ""
(new Date("5/1/2010")).localeFormat("MMMM")
returns ""
(new Date("6/1/2010")).localeFormat("MMMM")
returns ""
(new Date("7/1/2010")).localeFormat("MMMM")
returns ""
(new Date("8/1/2010")).localeFormat("MMMM")
returns ""
(new Date("9/1/2010")).localeFormat("MMMM")
returns ""
(new Date("10/1/2010")).localeFormat("MMMM")
returns ""
(new Date("11/1/2010")).localeFormat("MMMM")
returns ""
(new Date("12/1/2010")).localeFormat("MMMM")
returns ""
Please provide any additional information below. Attach a screenshot if
possible.
searched more in depth, seems if you put a regexp in a var and you want to
execute
this an unwanted result is returned, the regex isn't reset.
example FF:
var tokenRegExp =
/dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|s|tt|t|fff|ff|f|
zzz|zz|z/g
tokenRegExp.exec("MMMM") => ["MMMM"]
tokenRegExp.exec("MMMM") => null
tokenRegExp.exec("MMMM") => ["MMMM"]
...
in Chrome:
var tokenRegExp =
/dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|s|tt|t|fff|ff|f|
zzz|zz|z/g
tokenRegExp.exec("MMMM") => null
tokenRegExp.exec("MMMM") => ["MMMM"]
tokenRegExp.exec("MMMM") => null
tokenRegExp.exec("MMMM") => null
tokenRegExp.exec("MMMM") => null
...
it stays null until we change the exec parameter: ex:
...
tokenRegExp.exec("MMMM") => null
tokenRegExp.exec("MMM") => null
tokenRegExp.exec("MMMM") => ["MMMM"]
tokenRegExp.exec("MMMM") => null
tokenRegExp.exec("MMMM") => null
...
It is unable to change modify this line of code because it is auto
generated by ASP
in a ScriptResource.axd file when using Ajax
Regards,
Bart
My solution:
Replace all methods like new Date().format("") or new
Date().localFormat("") to
getLocalFormattedDate(new Date(), "")
function getLocalFormattedDate(date, format) {
var i = 0;
var fmt = date.localeFormat(format);
/* Google Chrome bug 5.0.375.55 */
while (fmt == "") {
date.localeFormat("yyyy/MM/dd hh:mm:ss:ffff");
fmt = date.localeFormat(format);
i = i + 1;
if (i > 5) { throw("Error retrieving date, restart your
browser!"); }
}
return fmt;
};
The following code will cause exec to return null after 4th iteration,
while in all other browsers will return "a". The code is extracted from MS
ASP.NET AJAX framework and this behavior of RegEx object in Chrome cause
any client-side new Date call after second time to return empty string as
result.
for (var i = 0; i < 5; i++)
{
alert(/a/g.exec("a"));
}
OR
var regEx = new RegExp(/a/g);
for (var i = 0; i < 5; i++)
{
alert(regEx.exec("a"));
}
The issue in RegEx cause the following ASP.NET AJAX code to fail:
var value = new Date(1230876000000); //-- value returned from service/for
example/
alert(String.localeFormat("{0:d}", value)); //-- 1/2/2009
alert(String.localeFormat("{0:d}", value)); //-- 1/2/2009
alert(String.localeFormat("{0:d}", value)); //-- return empty string
@bart.peremans - I guess you can override _getTokenRegExp of Date object to
overcome this temporary - at least it works for me:
Date._getTokenRegExp = function Date$_getTokenRegExp()
{
return new RegExp(/dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|
s|tt|t|fff|ff|f|zzz|zz|z/g);
}
Comment #6 on issue 45469 by ag...@chromium.org: Date.localeFormat doesn't
return a value
http://code.google.com/p/chromium/issues/detail?id=45469
This is indeed a bad regression. We will find the cause and get a patch
ready tomorrow. Thanks for the report!
Fixed on v8 bleeding edge and on 2.1 branch.
Comment #9 on issue 45469 by ag...@chromium.org: Date.localeFormat doesn't
return a value
http://code.google.com/p/chromium/issues/detail?id=45469
Yes, this is fixed.