Skip to main content
Participant
August 16, 2020
Question

PDF Javascript Issue With Date Selector

  • August 16, 2020
  • 1 reply
  • 4651 views

I have a PDF with some javascript that's used to calculate the time remaining in a mortgage contract, in years in months, as of the current date. The formula is:

 

Current Date - (Date Loan Closed + # years in the contract).

 

I have the formula calulating correctly, but for some reason, when the dates are entered then changed, whether entered manually or via the drop down, the "Remaining" portion spits out a single, random number. I have the date format set to mm/dd/yyyy.

 

For example, if a user entered "7/13/2005," I have a pop-up saying that the format needs to be corrected (since in this example, there needs to be a zero in front of 7). Once this change occurs, the "Remaining" calculation becomes incorrect. I'm not 100% that this glitch is caused from the date fields, but I can't seem to replicate it any other way.

 

I've attached a video of the error occuring so it might make more sense, as well as the pdf.

This topic has been closed for replies.

1 reply

ls_rbls
Community Expert
Community Expert
August 17, 2020

The first observation is that this PDF was created in Adobe Illustrator and exported to PDF.

 

It has a few design glitches and is acting very sluggish.  

 

The second observation is the scripts used to calculate dates are incorrect.  If you're using util.scand to process a date  object, once you're done applying your date script calculations,  you need to convert it back to a date string  object with util.printd or it won't work correctly.

 

You're also missing the appropriate arithmetic between  year, month, days, hours, minutes and miliseconds for accurate results.

 

 

Here's an example:

 

 

var LoanClosed = this.getField("DateLoanClosed").value;
var remaining = this.getField("RemainingYrs").value;
var d = util.scand("mm/dd/yyyy", LoanClosed);  
    d.setDate(d.getDate() - remaining);  
    event.value = util.printd("mm/dd/yyyy", d);  

 

 

 

And in order to calculate the correct difference in days between two dates you need  a different script.

 

You won't need a validattion script if you apply the correct arithmetic and use a date picker for the users, don't let them enter a date manually , unless you apply an arbitrary mask if there's a need for entering dates manually on each field.

 

The script below calculates difference in days between two given dates:

 

 

 

var strStart = this.getField("StatementDate").value;
var strEnd = this.getField("DateLoanClosed").value;

if ( ( strStart != "") && ( strEnd =="") )event.value = "day(s):"

 else if(strStart.length && strEnd.length)
{
  var dateStart = util.scand("mm/dd/yyyy",strStart);
  var dateEnd = util.scand("mm/dd/yyyy",strEnd);
  var diff = dateEnd.getTime() - dateStart.getTime();
  var oneDay = 24 * 60 * 60 * 1000;
  var days = ("day(s):");

  if (diff < "0")  event.value = "";
  else event.value = days;
  
}

else event.value = "";

 

 

It would be easier if you just reflect the difference in total month remaining , not year +months which is why you're getting the errors.

 

 

Participant
August 17, 2020

Thanks for the reply. Here's a couple of my questions:

 

- For the design aspect, I did the first half of the first page in Illustrator and converted it to PDF. The second half of the first page and the entire second page were all created in Acrobat. Is this what's causing the sluggishness?

 

- "The second observation is the scripts used to calculate dates are incorrect.  If you're using util.scand to process a date  object, once you're done applying your date script calculations,  you need to convert it back to a date string  object with util.printd or it won't work correctly."

How do I convert is back? Do I just replace "util.scand" with "util.printd" in "event.value ="?

 

- How do I only allow dates to be selected with the date picker? I don't want any manual additions.

 

- The "Remaining" section is more for visual presentation to loan officers so they don't have to bust out a calculator to divide the number of months remaining by 12. With that being said, if I wanted to keep this field, would I just replace the formula to take the total months remaining divided by 12 to get the number of years, then allocate the fraction remaining for the number of months?

 

I really appreciate you taking time to answer my questions! I'm just starting to learn Javascript, so your advice is awesome!