Skip to main content
Participant
June 27, 2021
Answered

How to auto-populate a text field from Date object in PDF form

  • June 27, 2021
  • 2 replies
  • 1121 views

Hello! 

 

I have created a PDF fillable form and want the text field "EDOAyrs" from the date they selected in "dd-mmm-yyyy" from the date object, here is my code:

event.value = "";
var EDOAValue = this.getField("EDOA").value;
var TodayValue = this.getField(new Date()).value;
var EDOAValue, TodayValue,diff;

if (EDOAValue!="") {
var EDOA = util.scand("dd-mmm-yyyy", EDOAValue);
var today = util.scand("dd-mmm-yyy", new Date());
var age = today.getTime() - EDOA.getTime();

// compute age in milliseconds
var nAgeMilliseconds = today.getTime() - EDOA.getTime();
var EDOAyrs = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);
event.value = Math.floor(EDOAyrs);

}

 

And here is the error msg:

InvalidSetError: Set not possible, invalid or unknown.
Event.value:1:AcroForm:EDOB:Annot1:MouseEnter:Action1

MissingArgError: Missing required argument.
Util.scand:4:AcroForm:EDOAyrs:Calculate
===> Parameter cDate.

 

Please help! Thanks.

This topic has been closed for replies.
Correct answer Adeline5DBB

So that's three different fields? One for service years, one for age and one for years until retirement? That's doable, but it's three different scripts, put in three different places and each one different. I can't tell if you were trying to combine these three into one, but that won't work. Looking at your code, which I think is the age part: I'd start by taking out the two references to TodayValue, they are quite wrong and not used in the answer.


Thank you for pointing it out. Indeed the earlier coding is not correct.

I have corrected using the copy of the code online and it works now:

event.value = "";
var eDOAValue = this.getField("exec_DOA").value;

if (eDOAValue!="") {

var eDOA = util.scand("dd-mmm-yyyy", eDOAValue);
var today = new Date();

// compute years on full year only

var exec_DOAyrs = today.getFullYear() - eDOA.getFullYear();

// adjust for months before month of birth

if (today.getMonth() < eDOA.getMonth()) exec_DOAyrs = exec_DOAyrs - 1;

// adjust for today's month equal to dob's month and today's date before the date of birth

if ((today.getMonth() == eDOA.getMonth()) && (today.getDate() < eDOA.getDate())) exec_DOAyrs -= 1;

event.value = exec_DOAyrs;

}

 

Thanks!

2 replies

Legend
June 27, 2021

I have to say that error is pretty certain from this line:


var TodayValue = this.getField(new Date()).value;

 

What is your aim for this line? If you just copied it from somewhere, refer back and check what the original said.

Participant
June 27, 2021

Sorry for not so good at this scripting. Yes, am trying to figure out from the web scripting on computing age from 2 dates.

What I am trying to achieve is when the user inputs their date of appointment from the "Calendar" drop box, the field will compute the # of service years. And likewise, when they input their DOB from the calendar drop box, a field will compute their age; and when the desired date of retirement is input using the Calendar drop box, a field will compute the countdown of # of years from the current year.

Legend
June 28, 2021

So that's three different fields? One for service years, one for age and one for years until retirement? That's doable, but it's three different scripts, put in three different places and each one different. I can't tell if you were trying to combine these three into one, but that won't work. Looking at your code, which I think is the age part: I'd start by taking out the two references to TodayValue, they are quite wrong and not used in the answer.

try67
Community Expert
Community Expert
June 27, 2021

Change this line:

var today = util.scand("dd-mmm-yyy", new Date());

To:

var today = new Date();

Participant
June 27, 2021

Thank you, but it is still not working,

The error msg:

TypeError: this.getField(...) is null
3:Field:Calculate

try67
Community Expert
Community Expert
June 27, 2021

That means you specified an incorrect field name.