Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I should add that I want it to say "years" and "months" after the corresponding number of years and months
Copy link to clipboard
Copied
Try searching the forum. This issue was discussed here many times and there are full codes posted for it.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
The remainder of the the calculation is what you need for months.
var nMonthsLeft = Math.floor((nAgeYears - event.value) * 12);
Find more inspiration, events, and resources on the new Adobe Community
Explore Now