Skip to main content
Participating Frequently
December 16, 2022
Answered

if - then: 2 different dates to calculate age

  • December 16, 2022
  • 1 reply
  • 6063 views

I would like to calculate someone's age based upon whether a "permission" date is entered. If no permission date is entered in the "Permission" field, then the age is calculated on today's date and is displayed in the "Age" field (where I am writing the code). If a permission date is entered in the "Permission" field, then the age is calculated using the date entered in the "Permission" field and is displayed in the "Age" field. If not too much trouble, I would like the age to display as years & months (example: 11-10). Thank you in advance for help!!! Dave

This topic has been closed for replies.
Correct answer bebarth

I did run into 1 small issue. Even when I delete/clear both the permission date and the date of birth, the "Student Age" and "Less than 12" fields retain the results from when previously calculated? I can certainly live with this small issue by waiting until I enter another student's information in the same place, but if there is a quick fix, that would be better? Thanks again!


Sorry for the delay!

I just added 2 lines at the end of the script for resetting the fields when the date of birth is empty, but that worked fine for me with the "Clear Form" button.

Please use this new file and let me know.

@+

1 reply

Thom Parker
Community Expert
Community Expert
December 17, 2022

Have you looked at all the other posts on calculating age?  There must be hundreds. 

 

Here's one that provides an age calc for the current date. It's a good place to start. 

https://community.adobe.com/t5/acrobat-discussions/calculating-age-based-dob-and-today-s-date/m-p/12789562

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Eric Dumas
Community Expert
Community Expert
December 17, 2022

I discovered an issue with this automatic age calculation in a project i worked on recently:

  • the form for a patient is to follow their progress over 3 years. 
  • The first visit works perfectly, age = today's date - date of birth
  • Issue: every time the physician/secretary opens the file, the age is recalculated. Which is correct as per formula, but then we lose the actual age of each visit.
try67
Community Expert
Community Expert
December 17, 2022

You need to move the code to the validation script of the DOB field, then. That way it will only update once that field's value is changed.