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

Calculate Age in Years AND Months from Date of Birth

New Here ,
Feb 06, 2020 Feb 06, 2020

I am needing to calculate the age in years and months from a date of birth field. The date of birth is in MM/DD/YYYY format. My date of birth field is called "Date of Birth" I am new to Adobe and coding so I appreciate any help. 

TOPICS
How to
3.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
New Here ,
Feb 06, 2020 Feb 06, 2020

I should add that I want it to say "years" and "months" after the corresponding number of years and months

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 ,
Feb 06, 2020 Feb 06, 2020

Try searching the forum. This issue was discussed here many times and there are full codes posted for it.

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 ,
Feb 06, 2020 Feb 06, 2020

Wow thank you for the helpful post. I have searched several threads and have not found one that has worked. Most of the threads have had other stipulations that I do not need. I've also struggled with substituting my "Date of Birth" field rather than the field titles others are using. 

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 ,
Feb 06, 2020 Feb 06, 2020

Date and time calculations are more difficult that regular calculations because dates and times have a complex relationship. So you need to work a bit harder to script them.

You can read about working with dates and times here:

Working with date and time in Acrobat JavaScript - Part 1 of 3

Working with date and time in Acrobat JavaScript - Part 2 of 3

Working with date and time in Acrobat JavaScript - Part 3 of 3

https://www.pdfscripting.com/members/Date-and-Time-Handling.cfm

 

Let us know when you've worked up a rough script and we can help you refine it.

 

 

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
New Here ,
Feb 06, 2020 Feb 06, 2020

Thank you. I have worked up a few scripts that I haven't been able to work so I will post them a little later. 

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 ,
Feb 06, 2020 Feb 06, 2020

Are you trying out your scripts first in the Console Window? This is the best place to try out scripting ideas, to find out what works.

You'll find a tutorial for it here:

https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro

 

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
New Here ,
Feb 07, 2020 Feb 07, 2020

I have this worked up and it will get me the years, but I'm struggling to convert it to years and months. Any help is greatly appreciated as it's the last thing I need for this document.

 

var dobValue = getField("Date of Birth").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
var age = today.getTime() - dob.getTime();
var nAgeMilliseconds = today.getTime() - dob.getTime();
var nAgeYears = ( age / (1000 * 60 * 60 * 24 * 365.2425));
event.value = Math.floor(nAgeYears);
}

 

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 ,
Feb 07, 2020 Feb 07, 2020
LATEST

The remainder of the the calculation is what you need for months.

 

var nMonthsLeft = Math.floor((nAgeYears - event.value) * 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