Page 1 of 1

javascript bug?

Posted: Wed Mar 28, 2007 5:27 pm
by Burrito
I'm reworking my JS calendar and I think I may have come across a bug.

I am parsing an integer out of a string to get the selected day and month...for some reason 08 and 09 when parsed go to 0 whereas every other number I've tried works fine.

I whipped up this little sample so you can try it yourself. Am I grossly overlooking something?

Code: Select all

<script>
field = "07/09/2004";
curDate = field.split("/");
curMon = parseInt(curDate[0]);
curYear = parseInt(curDate[2]);
curDay = parseInt(curDate[1]);
alert(curDate[0]);
alert(curMon);
</script>
that should work because it's not 08 or 09
but this

Code: Select all

<script>
field = "08/09/2004";
curDate = field.split("/");
curMon = parseInt(curDate[0]);
curYear = parseInt(curDate[2]);
curDay = parseInt(curDate[1]);
alert(curDate[0]);
alert(curMon);
</script>
returns a 0 for the integer value???????

any ideas?

Posted: Wed Mar 28, 2007 5:31 pm
by Burrito
just for the hell of it I tried parseFloat and that works..... 8O

very very weird

Posted: Wed Mar 28, 2007 5:34 pm
by Kieran Huggins
parseInt seems to be interpreting "08" as octal. You can force decimal with:

Code: Select all

<script type="text/javascript">
field = "08/09/2004";
curDate = field.split("/");
curMon = parseInt(curDate[0],10);
curYear = parseInt(curDate[2],10);
curDay = parseInt(curDate[1],10);
alert(curDate[0]);
alert(curMon);
</script>

Posted: Wed Mar 28, 2007 5:34 pm
by RobertGonzalez
That is feaking wierd. I threw a single digit at it and it came out right. But double, even triple digit, still results in 0 for 08, 008, 09 or 009. That is amzaing.

Posted: Wed Mar 28, 2007 5:36 pm
by RobertGonzalez
Kieran Huggins wrote:parseInt seems to be interpreting "08" as octal. You can force decimal with:
Dude, where'd you learn that? That is pretty nifty.

Posted: Wed Mar 28, 2007 5:38 pm
by Kieran Huggins
Octal numbers are denoted with a leading '0', hex is a leading '0x' - since numbers < 8 are identical in octal and decimal, it wasn't an issue until August.
Everah wrote:Dude, where'd you learn that? That is pretty nifty.
Honestly I have no idea - probably read it on Crockford's site. I just had a gut feeling....

Posted: Wed Mar 28, 2007 5:56 pm
by RobertGonzalez
Sweet. I really need to brush up on my Javascript skills.

Posted: Thu Mar 29, 2007 8:31 am
by feyd
It may have been from this bad boy: viewtopic.php?t=62578&highlight=octal