Skip to main content
Known Participant
September 28, 2020
Answered

Calculating Age

  • September 28, 2020
  • 2 replies
  • 7171 views

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

This topic has been closed for replies.
Correct answer Nesa Nurani

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.

 

 

 

 


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.

2 replies

ls_rbls
Community Expert
Community Expert
September 29, 2020

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:

 

 

Known Participant
September 29, 2020

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.

ls_rbls
Community Expert
Community Expert
September 29, 2020

Change this line :

 

 

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

 

 

To:

 

 event.value = Math.floor(age);

 

 

Let me know how it goes.

ls_rbls
Community Expert
Community Expert
September 29, 2020

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