Skip to main content
toddr14813053
Inspiring
June 4, 2019
Answered

Age calculation in months or years

  • June 4, 2019
  • 2 replies
  • 3872 views

I am using a calculation script to calculate age from a DOB field. It works just fine for the calculation and I can manipulate it to calc either in months or years. The problem is I want to have any ages < 1 year old to show the age in months and ages > 1 year old to show the age in years. Hopefully that makes sense. I have been searching through the existing threads and can't seem to find anything to help this specific need. If there a way to do this - maybe an additional IF clause? Here is my current calculation script.

event.value = "";

var dobValue = getField("Kid.DOB").value;

if (dobValue!="") {

var dob = util.scand("mm/dd/yyyy", dobValue);

var today = new Date();

// compute age in milliseconds

var age = today.getTime() - dob.getTime();

// convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

// truncate to whole years and adjust for binary floating point error

event.value = Math.floor( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

}

If I want calculate in months, I change the 365.2425 to 30.436.

This topic has been closed for replies.
Correct answer toddr14813053

Ok, the parenthesis are balanced, but there is an error.

"ReferenceError: age is not defined"

The "nAgeYears" calc contains a variable that doesn't exist.

You need to do some debug.

Here's an article that will help

https://acrobatusers.com/tutorials/how-to-debug-your-script

And a video

The Acrobat JavaScript Console Window - YouTube

Both are a little out of date, but the content is still valid.


Ok, I found the missing variable and that what was causing the problem. Here is the final script for anyone who wants it.

event.value = "";

var dobValue = getField("Kid.DOB").value;

if (dobValue!="") {

var dob = util.scand("mm/dd/yyyy", dobValue);

var today = new Date();

var age = today.getTime() - dob.getTime();

// compute age in milliseconds

var nAgeMilliseconds = today.getTime() - dob.getTime();

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

if(nAgeYears < 1)

{// Change to months (tricky because you have to handle new year crosing)

     var nAgeMonths = today.getMonth() - dob.getMonth();

     if(nAgeMonths < 0)

         nAgeMonths += 12;

     event.value = Math.floor(nAgeMonths);

}

else

    event.value = Math.floor(nAgeYears);

}

2 replies

Participant
January 3, 2021

Hi, Everyone!  

 

Thanks for all of this valuable information!  I took a class on this in college, and my brain exploded.  I'm much better at troubleshooting physical machines.  Anyway, I digress..

 

I have tried the sript offered by todd, but while I receive no errors, my age cell populates nothing.  I think I'm missing a script in my DOB cell.  I changed the DOB title to what mine is called to no avail.  Could someone please help?  I just need to calcluate the Age based off of the user's input of the DOB.

 

Thank you in advance for any help!

try67
Community Expert
Community Expert
January 3, 2021

Did you check the JS Console? Did you update the value of the DOB field after applying the calculation script?

If the answer is yes to both questions and it's still not working, please share the actual file with us for further help with this issue.

try67
Community Expert
Community Expert
June 4, 2019

So the current result is in years? If so you can add an if-statement that if it's less then 1, multiply the value by 12 to get the number of months. However, for that you will need to remove the Math.floor command, as that rounds the result down...

toddr14813053
Inspiring
June 4, 2019

I'm still new to JavaScript and since I borrowed this script from someone else, I'm going to need help replacing that math.floor command and adding the IF statement. I've tried some probably very bad attempts to add another IF and haven't been able to make anything work. I've also tried using the following script, but still can't get the age calc I want. Which scrip would be better? This one doesn't have the math.floor command in it, but would still need the additional IF statement.

event.value = "";

dobValue = getField("Kid.DOB").value;

if (dobValue!="") {

dob = util.scand("mm/dd/yyyy", dobValue);

today = new Date();

age = today.getFullYear() - dob.getFullYear();

if (today.getMonth() < dob.getMonth())

age;

else if (today.getMonth()== dob.getMonth())

age;

else if (today.getDate() < dob.getDate())

age;

else if (today.getDate() == dob.getDate())

age;

event.value = age;

}

Thom Parker
Community Expert
Community Expert
June 5, 2019

The first thing you need to do is decide on the conditions for the various results you want, Then figure out the calculations. It sounds like all you need to know is if the "age" is less than a year, in order do determine how the result is displayed.  The code in your first post is closer to the mark.

Here is an outline using the previous calculation

var nAgeMilliseconds = today.getTime() - dob.getTime();

var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

if(nAgeYears < 1)

{// Change to months (tricky because you have to handle new year crosing)

     var nAgeMonths = today.getMonth() - dob.getMonth();

     if(nAgeMonths < 0)

         nAgeMonths += 12;

     event.value = Math.floor(nAgeMonths);

}

else

    event.value = Math.floor(nAgeYears);

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often