SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict ownersite's Avatar
    Join Date
    Jul 2001
    Posts
    222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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!
    F150online- 102,000 users strong and growing...
    Ownersite.com
    - tools to enhance vehicle ownership

  2. #2
    SitePoint Addict BenANFA's Avatar
    Join Date
    Apr 2003
    Location
    Bath, UK
    Posts
    353
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #3
    Drupaler bronze trophy greg.harvey's Avatar
    Join Date
    Jul 2002
    Location
    London, UK
    Posts
    3,258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this?
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    	<head>
    		<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 ) )
    				{
    					alert( "Please enter a valid 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 ) )
    				{
    					alert( "Please enter a number for payments" );
    					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;
    				if ( answer <= 0 ) alert( "All payments have been made." );
    				f.remaining_payments.value = ( answer <= 0 ) ? 0 : answer;
    			}
    		</script>
    	</head>
    	<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>
    Last edited by beetle; Jun 3, 2003 at 10:58.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Addict ownersite's Avatar
    Join Date
    Jul 2001
    Posts
    222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.)
    F150online- 102,000 users strong and growing...
    Ownersite.com
    - tools to enhance vehicle ownership

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Updated it to error-check that.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Addict ownersite's Avatar
    Join Date
    Jul 2001
    Posts
    222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote 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?
    F150online- 102,000 users strong and growing...
    Ownersite.com
    - tools to enhance vehicle ownership


Bookmarks

Posting Permissions

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