Jump to content

ParseInt bug?


FrostbiteXIII

Recommended Posts

Sorry, just so confused by this, and its such simple code I cant see what stupid thing I could be doing, but I have this:

var sReceivedDate = "08/05/2007";var nDayR = parseInt(sReceivedDate.substring(0,2));var nMonthR = parseInt(sReceivedDate.substring(3,5));var nYearR = parseInt(sReceivedDate.substring(6,10));alert("" + nDayR + "/" + nMonthR + "/" + nYearR);

If sReceivedDate = "01/05/2007" - "07/05/2007" it works fine.If sReceivedDate = "08/05/2007" - "09/05/2007" it says the day is 0.If sReceivedDate >= "10/05/2007" it works fine again (tried every one as far as 20).Has anyone seen this before or can you tell me what stupid thing I am doing?!Thanks in advance for any help!Bob

Link to comment
Share on other sites

Scratch that - looks like it is a bug!http://www.breakingpar.com/bkp/home.nsf/0/...7256C85006A6604http://support.microsoft.com/default.aspx?...kb;en-us;191434"In JavaScript placing a leading 0 in front of a number designates that number as octal. In octal, there is no 08 or 09. Therefore, these numbers are interpreted as 0."

Link to comment
Share on other sites

It's not a bug, that is specifically how parseInt works. You need to specify that the number is a base-10 number instead of relying on Javascript to autodetect what base it is. I like how Microsoft says the solution is to remove the leading 0, that is not the solution. The solution is just to tell parseInt that you are using a base-10 number.

var nDayR = parseInt(sReceivedDate.substring(0,2), 10);var nMonthR = parseInt(sReceivedDate.substring(3,5), 10);var nYearR = parseInt(sReceivedDate.substring(6,10), 10);

http://devguru.com/technologies/javascript/11465.asp

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...