Skip to main content
July 18, 2020
Answered

Calculating age in years from DOB and document date from separate fields for month, day and year

  • July 18, 2020
  • 2 replies
  • 2428 views

Full disclaimer: I know NOTHING about Javascript. I've copied and pasted the scripts of other geniuses and have tried to understand the logic behind their scripts but just can't seem to grasp it. Anyway, I have an age field on a form into which I want the age in years automatically calculated from the DOB and the document's date based on separate fields for month, day and year. Until now, I could only find the following script that calculates based on the current date. I don't want the current date but, instead, the date typed into three separate date fields for Month / Day / Year (fields are "MM", "DD" and "Year"). The current script is below:

 

var dob = util.scand("mm/dd/yyyy", this.getField("Date of Birth").valueAsString);

if (dob != null) {

var today = new Date();

var age = today.getFullYear() - dob.getFullYear();

var m = today.getMonth() - dob.getMonth();

if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

age--;

}

event.value = age;

}

else {

event.value = "";

}

if(event.value == 0)
event.value = "";

This topic has been closed for replies.
Correct answer try67

If any of the fields is empty the dob variable will be null and the field will be empty.

2 replies

July 23, 2020

Oh, wait. I now see that I wasn't clear in my OP. The dob field is one text field. The document date field is what is separated as MM, DD, Year. So I want the age to be the difference in years between the dob (already in mm/dd/yyyy format in one field) and the document's date (which is in three separate fields: MM, DD, Year). Currently it is grabbing the current date. 

July 23, 2020

So I figured it out using your recommendation and it worked! However, now I don't know how to keep the age blank if no dates are typed in. This is where I'm at:

 

var docString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;

var dob = util.scand("mm/dd/yyyy", this.getField("Date of Birth").valueAsString);

if (dob != null) {

var today = util.scand("mm/dd/yyyy", docString);

var age = today.getFullYear() - dob.getFullYear();

var m = today.getMonth() - dob.getMonth();

if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

age--;

}

event.value = age;

}

else {

event.value = "";

}

if(event.value == 0)
event.value = "";

 

Thanks! 

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 23, 2020

If any of the fields is empty the dob variable will be null and the field will be empty.

try67
Community Expert
Community Expert
July 18, 2020

Replace the first line of the code with this:

 

var dobString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;
var dob = util.scand("mm/dd/yyyy", dobString);

July 21, 2020

Thank you so much try67. I tried it but nothing calculated. The age box remains blank. Just using logical assumptions, I tried this and it also didn't work:

 

var docString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;

var dob = util.scand("mm/dd/yyyy", docString);

if (dob != null) {

var today = docString();

var age = today.getFullYear() - dob.getFullYear();

var m = today.getMonth() - dob.getMonth();

if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

age--;

}

event.value = age;

}

else {

event.value = "";

}

 

 

I don't know where to go from here or what else to try. By the way, I don't think I mentioned that I'm using Adobe Acrobat Pro XI on Windows 10.

try67
Community Expert
Community Expert
July 21, 2020

If there are no error messages in the JS Console then share the file with us for further help with it.