Basic JavaScript for a PDF

Basic JavaScript for a PDF
0

I have a text field (e.g. “Text1”) in a Nitro Pro (an Adobe Acrobat quasi-clone) PDF, that is formatted to hold a date (mm/dd/yy). I want a second text field (e.g. “Text2” also mm/dd/yy) to automatically be populated with a date that is 30 calendar days later, based on the date entered into the first text field by the user.

Nitro Pro only provides basic operations to be performed on the contents of a source field, and none of the available operations does what I need. However, Nitro Pro does allow lines of Javascript to be associated with the second field as “Custom calculation script,” but nothing I’ve tried even comes close to working (I have no Javascript experience).

Can anyone help with Javascript code that will serve this function, with these considerations?:

  • The source date in the first text field appears to be ASCII text, even though it’s formatted as a date.
  • As simple and with as few lines of code as possible

Dates are really finicky, and depend on your locale so this may or may not work.
And I’m not sure about how the “Custom calculation script” gives input and receives output, so I’m just going to write a function that takes a string Text1 as a parameter, and returns a string Text2 (both mm/dd/yy).

Also, can you confirm if mm/dd/yy means 03/27/20 or 3/27/20 (ie, are there leading zeroes)?

function plus30Days(Text1) {
  const date1 = new Date(Text1);
  const date2 = new Date(
    date1.getFullYear(),
    date1.getMonth(),
    date1.getDate() + 30
  );
  const mm = (date2.getMonth() + 1).toString().padStart(2, '0');
  const dd = date2.getDate().toString().padStart(2, '0');
  const yy = date2.getFullYear().toString().slice(-2);
  const Text2 = `${mm}/${dd}/${yy}`;
  return Text2;
}

Calling the function:

plus30Days('03/27/20') // => '04/26/20'

Wow, looks very ominous. I have pasted it into Notepad++, so will see if I can make it work. Please don’t spend any more time on this, as you’ve obviously done more than enough already (thanks much for that). To your question: It doesn’t matter if I type in “2/23/20,” “02/23/20,” “2/23/2020” or “02/23/2020,” it gets displayed as “02/23/20.”
p.s. Maybe this will be the incentive I need to learn more about Javascript.