javascript bug?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

javascript bug?

Post 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?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

just for the hell of it I tried parseFloat and that works..... 8O

very very weird
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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>
Last edited by Kieran Huggins on Wed Mar 28, 2007 5:34 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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....
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sweet. I really need to brush up on my Javascript skills.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It may have been from this bad boy: viewtopic.php?t=62578&highlight=octal
Post Reply