I find this to work effectively, Below I am getting a date from an asp.net textbox and then getting today's date. I then send both dates through a function to get the number of months that I need to do my calculation (be aware that the code uses Infragistics libraries (if you intent to copy and paste, replace those calls with standard JavaScript calls):
function calculateMonthlyRepayment()
var tStartDate = $find('dp_Start_Date');
var startDate = tStartDate.get_value();
var today = new Date();
var numMonths = monthDiff(startDate, today);
var rate = igedit_getById('WebNumericEdit_InterestRate').getValue() * 0.01;
var intRate = (rate / 100) / 12;
var principal = igedit_getById('WebCurrencyEdit_Proposed_Owing').getValue();
var repayment = (principal * (Math.pow((1 + intRate), numMonths)) * intRate / (Math.pow((1 + intRate), numMonths) - 1));
igedit_getById('txt_Monthly_Repayment').setValue(repayment);
}
function monthDiff(d1, d2) {
var months;
months = (d2.getFullYear() - d1.getFullYear()) * 12;
months -= d1.getMonth() + 1;
months += d2.getMonth();
return months <= 0 ? 0 : months;
}