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

if - then: 2 different dates to calculate age

New Here ,
Dec 16, 2022 Dec 16, 2022

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

TOPICS
How to , JavaScript , PDF forms
5.0K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
Dec 20, 2022 Dec 20, 2022

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.

@+

View solution in original post

Translate
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 ,
Dec 16, 2022 Dec 16, 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/12...

 

 

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

Translate
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 ,
Dec 17, 2022 Dec 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.
Translate
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 ,
Dec 17, 2022 Dec 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.

Translate
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 ,
Dec 18, 2022 Dec 18, 2022

I have looked at many of the available posts. I do not have enough knowledge about code to piece things together from different posts so they will work for my project. For now, instead of leaving the "Permission" field blank, I will just enter "today's date" into "Permission" field until I have permission to evaluate the child, then change the date in the "Permission" field to the date permission was granted. Regarding displaying age in years and months: For now, I will have the "Age" field just display in years (11) instead of years & months (11-10). Simple and less automated will be better until I have time to learn more about code. 🙂

Thanks for your suggestion...

Translate
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 ,
Dec 18, 2022 Dec 18, 2022

I have looked at many of the available posts. I do not have enough knowledge about code to piece things together from different posts so they will work for my project. For now, instead of leaving the "Permission" field blank, I will just enter "today's date" into "Permission" field until I have permission to evaluate the child, then change the date in the "Permission" field to the date permission was granted. Regarding displaying age in years and months: For now, I will have the "Age" field just display in years (11) instead of years & months (11-10). Simple and less automated will be better until I have time to learn more about code.

Thanks for your suggestion...

Translate
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 ,
Dec 18, 2022 Dec 18, 2022

Here is the example from a previous one I did some times ago.

I hope it will help you!

@+

Translate
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 ,
Dec 19, 2022 Dec 19, 2022

You example seems to be doing what I want; however, I cannot find where you have the code written to do the tasks? I am opening the document and going to "Prepare Form". Then I opened "age" and looked under "Calculate" and "Validate", but I don't see any code... and yet it works 🙂 Where is everything hidden? Thanks!

Translate
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 ,
Dec 19, 2022 Dec 19, 2022

There is a function defined in document-level of my file:

Capture d’écran 2022-12-19 à 23.44.10.pngexpand image

and a run JavaScript on Blur action for both birthday and permission fields

Capture d’écran 2022-12-19 à 23.44.50.pngexpand image

@+

Translate
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 ,
Dec 19, 2022 Dec 19, 2022

I do see java scripting of some sort when I right click on the "page". It may be important to point out that there are 40 students per page where these calculations are taking place.SampleCut.jpgexpand image

Translate
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 ,
Dec 19, 2022 Dec 19, 2022

As you have 40 studients, you certainly have fields with indices, so the script will be a bit different!

Could you share a file and I'll show you how do that...

@+

Translate
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 ,
Dec 19, 2022 Dec 19, 2022

Of course I would be happy to share the form 🙂

Thank you for the help you have provided so far!!!

Translate
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 ,
Dec 20, 2022 Dec 20, 2022

Hi,

Here is my copy!

I hope you'll understant what I did... else let me know.

@+

Translate
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 ,
Dec 20, 2022 Dec 20, 2022

WOW!!! Everything works great! You wanted me to let you know if I understood what you did... I looked at the Java scripting and understood bits and pieces, no clue about indices... guess I will need to invest time into a class if I do much more with this type of form 🙂

Thank you so much for the help you gave me! I hope the holidays are great to you!!!

Dave

Translate
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 ,
Dec 20, 2022 Dec 20, 2022

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!

Translate
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 ,
Dec 20, 2022 Dec 20, 2022

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.

@+

Translate
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 ,
Dec 21, 2022 Dec 21, 2022
LATEST

This one works perfectly!!! Thank you again so much for your tremendous help! Happy Holidays to you and yours!!! Dave

Translate
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