• 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

Explorer ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

I know there are a ton of questions just like this out there, but I literally know NOTHING about JavaScript.  I have a "Date of Birth" field and a "Date of Death" field.  What calculation can I use to find age?  Everything else I see uses today's date, but I need to use another date field, ie. "Date of Death", and I don't know enough to manipulate the code to make it work.

 

Please help, I'm clueless with JavaScripts...

TOPICS
Edit and convert PDFs , General troubleshooting , How to , PDF forms

Views

5.6K

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
2 ACCEPTED SOLUTIONS
Community Expert ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

Sorry I made a mistake in my prior reply.

 

 I missed the part of  calculating the miliseconds in the time difference "diff" variable.

 

Here's the finished script:

 

//Custom calculation script to calculate Age based on date of birth and date of death

var birth= this.getField("Date of Birth").value;

var death = this.getField("Date of Death").value;

 if (birth.length && death.length) {

     var dateBirth = util.scand("mmmm d, yyyy", birth);
  
     var dateDeath = util.scand("mmmm d, yyyy", death);

//calculate the difference in miliseconds between the death date and birth date

     var diff = (dateDeath.valueOf() - dateBirth.valueOf())/1000;
  
// calculate years

     var age =   (((diff / 60) / 60) / 24) / 365;

//round off the total using  util.printf() method

  event. value = util.printf("%0.0f", age);
 
  }

 

See slide below:

 

age.png

 

View solution in original post

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 ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Here are some info on how to calculate leap years https://www.htmlgoodies.com/html5/javascript/learn-how-to-use-javascript-dates-and-leap-years.html 

I'm not good at calculating dates but maybe you could use something like this:

if(age >= 4){var age2 = age/4;}
var age3 = age2*86400;
var age4 = ((((diff-age3) / 60) / 60) / 24) / 365

event. value = Math.floor(age4);

this worked for me unless diff is less then 4 years, although this intrigue me, unfortunately I don't have much time to play with this but hope you can worked something from it.

View solution in original post

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 ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

You can try something like this:

 

//calculate Age based on date of birth and date of death

var birth= this.getField("Date of Birth").value;
var death = this.getField("Date of Death").value;

 if (birth.length && death.length) {

  var dateBirth = util.scand("mmmm d, yyyy", birth);
  var dateDeath = util.scand("mmmm d, yyyy", death);
  var diff = dateDeath.getTime() - dateBirth.getTime();
  var age  = Math.round( (((diff / 60) / 60) / 24) / 365);

  event.value = age;

}

 

The Age needs to be rounded off to two or three number though.

 

I am still working on that.

 

This example is documented in the Adobe Acrobat DC SDK Using JavaScript in Forms, Developing Acrobat® Applications Using JavaScript™, " Date arithmetic" Page  94

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 ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

Sorry I made a mistake in my prior reply.

 

 I missed the part of  calculating the miliseconds in the time difference "diff" variable.

 

Here's the finished script:

 

//Custom calculation script to calculate Age based on date of birth and date of death

var birth= this.getField("Date of Birth").value;

var death = this.getField("Date of Death").value;

 if (birth.length && death.length) {

     var dateBirth = util.scand("mmmm d, yyyy", birth);
  
     var dateDeath = util.scand("mmmm d, yyyy", death);

//calculate the difference in miliseconds between the death date and birth date

     var diff = (dateDeath.valueOf() - dateBirth.valueOf())/1000;
  
// calculate years

     var age =   (((diff / 60) / 60) / 24) / 365;

//round off the total using  util.printf() method

  event. value = util.printf("%0.0f", age);
 
  }

 

See slide below:

 

age.png

 

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
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

It is working except that if the Date of Death is within six months and one week of the Date of Birth it counts the age as including that year.  For example with the Date of Death as 02/02/2020 and Date of Birth as 08/08/1997 it shows the age as 23, when it should really be 22. If I change the Date of Birth to 08/09/1997 it changes to 22.  It seems to round up the age if it is within six months and seven days.

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 ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Change this line :

 

 

  event.value = util.printf("%0.0f", age);

 

 

To:

 

 event.value = Math.floor(age);

 

 

Let me know how it goes.

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
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

It is much much closer, but it will still round the age up within a few days rather than months. Definitely better, but still a few days off and the greater the age the larger the error seems to become.  

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 ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

I know, handling basic arithmetic in JavaScript is very complicated and to get precise notations is a real pain in the neck .

 

Sometimes what I do is search for the real arithmetic formula (like the one done by hand) and then figure it out with the scripting to get the most accurate result. 

 

I am still working on this. Let me see what I can find.

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
Explorer ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Gotcha, I appreciate it a ton. Like I said, I know nothing as of now about JavaScript, so I greatly appreciate the help.

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 ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Based on the online tools,  I think the best way to work around the JavaScript problem  is to calculate the Age based on Birth and Death in terms of years and days.

 

See this online tool: https://rechneronline.de/year-of-birth/age.php

 

Using this method in your form will provide the most accurate result.

 

When done by hand here's how to: https://blog.eogn.com/2020/05/26/calculating-birth-dates-from-death-date-information/

 

But you'll notice that it is also going to be a little off in terms of days because of leap year, rollover dates, and the fact that not all months end on the 31st day. All of these things come into play when developing the date arithmetic. JavaScript doesn't make any these any easier due to the way it handles floating points, for example.

 

So we need to add an additional field for days, and possibly another field for the months using the arithmetic formula provided in my second link above.

 

 

 

 

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 ,
Sep 29, 2020 Sep 29, 2020

Copy link to clipboard

Copied

Here are some info on how to calculate leap years https://www.htmlgoodies.com/html5/javascript/learn-how-to-use-javascript-dates-and-leap-years.html 

I'm not good at calculating dates but maybe you could use something like this:

if(age >= 4){var age2 = age/4;}
var age3 = age2*86400;
var age4 = ((((diff-age3) / 60) / 60) / 24) / 365

event. value = Math.floor(age4);

this worked for me unless diff is less then 4 years, although this intrigue me, unfortunately I don't have much time to play with this but hope you can worked something from 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
Explorer ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

This worked! The only time it isn't right is if the the Date of Death and Date of Birth are the same date AND in a leap year. Other than that it seems to work great. Thank you!!

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 ,
Sep 30, 2020 Sep 30, 2020

Copy link to clipboard

Copied

LATEST

I was able to get the age in years worked around as well. It only gives you age in years though.

 

If a person is younger than one year old or younger than  one month old at the time of death it will only display 0, since it doesn't calculate age in months or days.

 

But changing the line ;

 

 event.value = Math.floor(age);

 

To:

 

event.value = util.printf("%d", age);

 

Seems to work pretty well.

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