Skip to main content
Jul 18, 2020 6:04 PM UTC
Answered

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

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Jul 23, 2020 8:26 AM UTC

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

9 replies

Jul 23, 2020 3:03 AM UTC

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. 

Translate
Jul 23, 2020 4:46 AM UTC

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! 

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Jul 23, 2020 8:26 AM UTC

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

Translate
try67
Community Expert
Community Expert
Jul 18, 2020 9:40 PM UTC

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);

Translate
Jul 21, 2020 1:26 AM UTC

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.

Translate
try67
Community Expert
Community Expert
Jul 21, 2020 8:03 AM UTC

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

Translate