Skip to main content
henrykh
New Participant
January 13, 2017
Answered

Auto populate a PDF with current year to next year using Javascript

  • January 13, 2017
  • 1 reply
  • 2395 views

is it possible to auto populate a PDF with the last 2 digits 20__ - 20__ with current year to next year every time I open up a PDF page? example 2017-2018?

This topic has been closed for replies.
Correct answer gkaiseril

That didn't work...

I have this JavaScript on Page Properties:

var f = this.getField("Today");

f.value = util.printd("yy", new Date());

so when the page pull up "20__ to 20__" comes up auto populated as "2017 to 20__".

Any way to add another script onto the above so it will show 2017 to 2018?


You need to add another field and then increment the year value by 1.

A possible script is:

var f = this.getField("Today");

f.value = util.printd("yy", new Date());

f = this.getField("NextYear");

f.value = Number(util.printd("yy", new Date())) + 1;

For this to work one needs to be careful about the type of variables, that is , there is a significant difference between a String variable and a Number variable and how the "+" operator works.

1 reply

try67
Adobe Expert
January 13, 2017

You can use this code to populate the whole thing into a field:

var now = new Date();

this.getField("Years").value = "" + now.getFullYear() "-" + (now.getFullYear()+1);

henrykh
henrykhAuthor
New Participant
January 13, 2017

I get a syntaxError: missing ; before statement 2: at line 3 when I put this in the JavaScript Editor. How do I correct that? Thanks for your help.

henrykh
henrykhAuthor
New Participant
January 13, 2017

You need to add another field and then increment the year value by 1.

A possible script is:

var f = this.getField("Today");

f.value = util.printd("yy", new Date());

f = this.getField("NextYear");

f.value = Number(util.printd("yy", new Date())) + 1;

For this to work one needs to be careful about the type of variables, that is , there is a significant difference between a String variable and a Number variable and how the "+" operator works.


That totally worked! Thank you gkaiseril and try67 for your help!