Closed
Bug 869089
Opened 12 years ago
Closed 12 years ago
javascript date no working for 8 november 2012 and 9 november 2012
Categories
(Firefox :: Untriaged, defect)
Tracking
()
RESOLVED
DUPLICATE
of bug 786135
People
(Reporter: csantos.pti, Unassigned)
Details
Attachments
(1 file)
937 bytes,
text/html
|
Details |
User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
Build ID: 20130409194949
Steps to reproduce:
<html>
<head>
<script type='text/javascript'>
function myFunction()
{
var d1=str2date('20121107');
var d2=str2date('20121108');
var d3=str2date('20121109');
var d4=str2date('20121110');
document.getElementById("p1").innerHTML='20121107='+d1.toString();
document.getElementById("p2").innerHTML='20121108='+d2.toString();
document.getElementById("p3").innerHTML='20121109='+d3.toString();
document.getElementById("p4").innerHTML='20121109='+d4.toString();
}
function str2date(d) {
var y = d.substring(0, 4);
var m = d.substring(4, 6);
var d = d.substring(6, 8);
var n = new Date(parseInt(y), parseInt(m)-1, parseInt(d));
return n;
}
</script>
</head>
<body>
<h1>Javascript Dates</h1>
<p id="p1"></p>
<p id="p2"></p>
<p id="p3"></p>
<p id="p4"></p>
<button type="button" onclick="myFunction()">Call new Date Test</button>
</body>
</html>
Actual results:
Javascript Dates
20121107=Wed Nov 07 2012 00:00:00 GMT+0000
20121108=Wed Oct 31 2012 00:00:00 GMT+0000
20121109=Wed Oct 31 2012 00:00:00 GMT+0000
20121109=Sat Nov 10 2012 00:00:00 GMT+0000
Expected results:
Javascript Dates
20121107=Wed Nov 07 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
20121108=Thu Nov 08 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
20121109=Fri Nov 09 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
20121109=Sat Nov 10 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
(This is output in Chrome and IE)
This happens because parseInt is being used without the second 'radix' parameter, which causes strings with a leading '0' to be treated as octal. So days '08' and '09' are converted to the number 0, since 8 and 9 are invalid octal digits.
See https://developer.mozilla.org/docs/JavaScript/Reference/Global_Objects/parseInt (specifically, the section "Octal Interpretations with No Radix").
This behaviour was changed for Firefox 21 though (see bug 786135), so your example should work as expected in version 21 and higher.
Attachment #745967 -
Attachment mime type: text/plain → text/html
Confirmed by the working range:
http://hg.mozilla.org/mozilla-central/pushloghtml?fromchange=0c45e6378f1f&tochange=677e87c11252
Status: UNCONFIRMED → RESOLVED
Closed: 12 years ago
Resolution: --- → DUPLICATE
Comment 3•12 years ago
|
||
Note that, despite parseInt's second argument being optional, you really should always supply it, precisely to avoid surprises like this one. See also:
http://whereswalden.com/2011/02/26/the-proper-way-to-call-parseint-tldr-parseintstr-radix/
You need to log in
before you can comment on or make changes to this bug.
Description
•