Posted in Uncategorized

Small JavaScript Gotcha

Date December 5th, 2007

Further to the previous post, I am using javascript to parse times in the form “09:00″ selected from a drop down list.

To get the hours and minutes, I was using the following code:

...

var theHours = parseInt(theTimeDDL.options[theTimeDDL.selectedIndex].value.substring(0,2));

var theMinutes = parseInt(theTimeDDL.options[theTimeDDL.selectedIndex].value.substring(3,5));

...

Unfortunately, the above will return 0 for the values of 08 and 09.

This is due to the fact that parseInt() will treat any string in the form 0x as an octal number, and 08 and 09 are not valid base-8 values.

To get around this, the call needs to be changed to include a second parameter of 10, to indicate a base-10 conversion.

[Via: Breaking Par Consulting, Inc.]

Leave a Reply

(required)

(required)