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

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

Guest
Jul 18, 2020 Jul 18, 2020

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
2.3K
Translate
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

Deleted User
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

...
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.

Translate
Community Expert ,
Jul 18, 2020 Jul 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);

Translate
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
Guest
Jul 20, 2020 Jul 20, 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.

Translate
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

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

Translate
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

Following line is not correct:

var today = docString();

 

docString is not a function.

Translate
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
Guest
Jul 22, 2020 Jul 22, 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. 

Translate
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
Guest
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 = 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
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

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

Translate
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

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?  

Translate
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
LATEST

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

Translate
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