# Thread: Calculating number of months between two dates?

1. ## Calculating number of months between two dates?

I have a form with two user-defined fields. The first field enables them to input the first payment date of a loan. The second field enables them to input the number of payments in that loan. There is a hidden field that holds today's date.

I want to dynamically calculate the "number of payments remaining" on that loan using Javascript. Unfortunately, it's not a simple as calculating the number of months between two dates and subtracting from the number of payments.

For instance, if the first payment is on 5/13/2003 and there are 60 payments, on 6/3/03 there are 59 payments remaining. On 6/14/03, there are 58 payments remaining.

Needless to say, this would also have to be Y2K compliant...

Any pointers, suggestions or sample code that someone out there is familiar with?

TIA!

2. Unfortunately, it's not a simple as calculating the number of months between two dates and subtracting from the number of payments.
But it almost is, if you calculate this number and then if the current date is greater than the payment date subtract 1 you will get the number of payments.

i.e. from above

6/2003 - 5/2003 = 1 month
payments = 60 - 1 = 59
if ( date >= 5 ) payments--

so on the 3rd payments = 59 but on the 14 payments = 58.

Do you have a method for getting the number of months between 2 dates yet ?

Also do you have to deal with dates that fall on the 29th, 30th or 31st of the month since this will confuse things too.

3. This may be useful.... I haven't trawled through it, but it seems to have a lot of functions and examples for date handling.... http://www.merlyn.demon.co.uk/js-date1.htm#TDO

4. Like this?
Code:
```<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<title>test</title>
<script type="text/javascript">
function getPayments( f )
{
var today = new Date();
var date  = new Date( f.loan_date.value );
if ( isNaN( date ) )
{
f.loan_date.select();
f.loan_date.focus();
return;
}
if ( today < date )
{
alert( "The date must be on or before today" );
f.loan_date.select();
f.loan_date.focus();
return;
}
if ( /[^\d]/.test( f.total_payments.value ) )
{
f.total_payments.select();
f.total_payments.focus();
return;
}
var difference = ( today.getFullYear() - date.getFullYear() ) * 12 + today.getMonth() - date.getMonth();
var answer	 = parseInt( f.total_payments.value, 10 ) - difference;
}
</script>
<body>
<form name="loan">
<label for="loan_date">Loan Date</label>
<input type="text" name="loan_date" id="loan_date" />
<br />
<label for="total_payments">Total Payments</label>
<input type="text" name="total_payments" id="total_payments" size="5" />
<br />
<label for="remaining_payments">Remaining Payments</label>
<input type="text" name="remaining_payments" id="remaining_payments" size="5" readonly="true" />
<br />
<input type="button" value="Calculate Remaning payments" onclick="getPayments( this.form );" />
</form>
</body>
</html>```

5. EXACTLY LIKE THAT!!!

Thanks beetle!!!!

(As I started testing it, there does appear to be one flaw...if you add a date in the future, it will add the number of months between now and then to the number of payment due. I should be able to fix that, though.)

6. Updated it to error-check that.

7. Originally Posted by beetle
Updated it to error-check that. [img]images/smilies/biggrin.gif[/img]
Perhaps I have mis-hacked it and accidentally removed this logic, but you will probably also want to add logic that says if the year and month are the same and today.getDate() is greater than date.getDate() then subtract one from the payment count. Did that make sense?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•