• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Jul 18, 2020 Jul 18, 2020

Copy link to clipboard

Copied

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 = "";

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Beginner , Jul 22, 2020 Jul 22, 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

...

Votes

Translate

Translate
Community Expert , Jul 23, 2020 Jul 23, 2020

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

Votes

Translate

Translate
Community Expert ,
Jul 18, 2020 Jul 18, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 20, 2020 Jul 20, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 21, 2020 Jul 21, 2020

Copy link to clipboard

Copied

Following line is not correct:

var today = docString();

 

docString is not a function.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 22, 2020 Jul 22, 2020

Copy link to clipboard

Copied

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! 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 23, 2020 Jul 23, 2020

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 03, 2021 Jan 03, 2021

Copy link to clipboard

Copied

Hi there!

 

Could someone please offer reference as to where this script should be entered?  I have tried properties>calculation>custom..., but nothing populates in the pdf preview.  The date of birth entered will be one string, so do I need to add the first MM, DD, YEAR portion to the script?  Also, do I need to do anything to the cell in which the user will input the DOB, or just make sure its title is correct in the script?  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2021 Jan 03, 2021

Copy link to clipboard

Copied

LATEST

You've already posted in another thread, so let's continue there. There's no need to ask in multiple places.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines