Javascript parseInt() may not do what you expect!
TweetI was shocked today to find I’d introduced a bug.
It manifested itself like this;

This might seem ok.
Except the “Number of repayments” is set by javascript as;
the number of days interest
less the number of days before repayment begins
Looking at the dates, the “number of days before repayment begins” should be 2.
But the difference is -6.
Something is wrong.
And the answer is here;
var date_from_field = function(field_id) {
var field = $(“#”+field_id);
var year, month, day;
var date_bits = field.val().split(“-“);
var year = parseInt(date_bits[0]);
var month = parseInt(date_bits[1]);
var day = parseInt(date_bits[2]);
return new Date(year,month-1,day);
};
I take a date field
<input type=”date” value=”2010-12-08” />
I grab the value
“2010-12-08”
I split it into year, month, and day
[“2010”, “12”, “08”]
I turn them into integers
[2010, 12, 0]
Then I make them into a date
new Date(2010,11,0); // Tue Nov 30 2010
No problem!
Except…
“2010-12-08” != Tue Nov 30 2010
The problem is
parseInt(“08”) == 0
parseInt(“08”, 10) == 8
“08” is 0 in octal
Always use parseInt(something, 10)