Skip to main content
Participating Frequently
March 3, 2022
Answered

Calculating Age based DOB and Today's date

  • March 3, 2022
  • 1 reply
  • 4387 views

Currently, these are the fields I have. 

DoB Field name: Date of Birth

Date Field name: Date of Today [Calculated using the following script] which is read-only so every time our front desk opens this document, the date is automatically filled. 

var fld = this.getField("Date of Today"); fld.value = util.printd("mm/dd/yyyy",new Date());

Age field name: Age

 

I am using Acrobat DC. I have my Age to "read-only" as well. Currently, I have this script running and nothing is populating on the Age fields.

event.value = "";
var dobValue = getField("Date of Birth").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
var age = today.getFullYear() - dob.getFullYear();
if (today.getMonth() < dob.getMonth()) age = age - 1;
if ((today.getMonth() == dob.getMonth()) && (today.getDate() < dob.getDate())) age -= 1;
event.value = age;
}

Help me out on this? I have absolutely no knowledge of Java at all. I am just following instructions on the web.

This topic has been closed for replies.
Correct answer Bernd Alheit

Use the script at calculation of field Age.

1 reply

Bernd Alheit
Community Expert
Community Expert
March 3, 2022

Where does you use the script?

Participating Frequently
March 3, 2022

I am placing the scripts in the Documents JavaScript

 

 

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
March 3, 2022

Use the script at calculation of field Age.