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

Age calculation in months or years

Explorer ,
Jun 04, 2019 Jun 04, 2019

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript

Views

2.5K

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 1 Correct answer

Explorer , Jun 06, 2019 Jun 06, 2019

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 (tr

...

Votes

Translate

Translate
Community Expert ,
Jun 04, 2019 Jun 04, 2019

Copy link to clipboard

Copied

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

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 ,
Jun 04, 2019 Jun 04, 2019

Copy link to clipboard

Copied

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;

}

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

I thought it was working but now it won't show the answer at all. I don't know what I did. Here is what I am using now. My machine might be acting up.

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

}

Am I missing something?

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

Yes, the "if(dobValue!="") does not have a matching parentheses. 

Did you look in the console window to see if any errors are reported? It would have indicated this exact syntax issue.

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

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

There are parenthesis and there was no syntax error. Everything is exactly like I posted it. The scrip just won't do the calculation at all with the way it's written now.

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 ,
Jun 05, 2019 Jun 05, 2019

Copy link to clipboard

Copied

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.

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

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 ,
Jun 06, 2019 Jun 06, 2019

Copy link to clipboard

Copied

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

}

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 ,
Jun 06, 2019 Jun 06, 2019

Copy link to clipboard

Copied

You just repeated the line that calcualates the age in milliseconds.  The nAgeMilliseconds parameter was already there, you should have either used it or removed the line.

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

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 ,
Jul 18, 2019 Jul 18, 2019

Copy link to clipboard

Copied

Ok Thom,

Now for an extension of this question. For ages that are  less than 1 month, I would like the calculation to show how many weeks old they are. I have used your original script as a starting point and the script does recognize when it should be showing weeks, but there is something wrong with it actually showing the number of weeks. For example, when nAgeMonths < 1, it will change the text part of the event value from Months to Weeks, but it will show 0 weeks instead of the actually number of weeks. I hope that makes sense. I think the problem may be in one of two areas:

var nAgeWeeks = today.getMonth() - dob.getMonth(); // shouldn't this be changed since we're not using complete months?

or

nAgeWeeks += 4; //should this be something other than 4? I've tried 52 but it still showed 0 weeks at the end.

Here is the entire script to look at

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 nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

if(nAgeYears < 1) {

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

     if(nAgeMonths < 0)

         nAgeMonths += 12;

     event.value = Math.floor(nAgeMonths) + " Months";

   

if(nAgeMonths < 1) {

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

    if(nAgeWeeks < 0)

        nAgeWeeks += 4;

    event.value = Math.floor(nAgeWeeks) + " Weeks";

    }

}

else

    event.value = Math.floor(nAgeYears) + " Years";

}

I hope all that makes sense.


Thanks, Todd

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 18, 2019 Jul 18, 2019

Copy link to clipboard

Copied

Hi Todd,

   Please post this to a new thread

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

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 ,
Jul 18, 2019 Jul 18, 2019

Copy link to clipboard

Copied

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, 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!

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

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.

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