Skip to main content
Participating Frequently
July 25, 2022
Answered

Calculate Age in Years AND Months

  • July 25, 2022
  • 2 replies
  • 2462 views

Hello, I need a script to calculate someone's age in years and months (e.g., 14 years and 5 months old).

 

I have searched way too long and have been able to get years to caculate with no problem (using the Date of Birth and current date), but cannot for the life of me figure out the months part.

 

I'm making a form so I can use either the current date or the date the form was completed (input by the user). I would prefer to use the date the form was completed but will use either. 

 

I also don't care either way if this is in one field or two (one for years and one for months). Right now I have two separarte fields set up since I was only able to get the years to calculate. Can someone please help? Please? 

 

Here is the script I found after searching and am using to calcualte years: 


event.value = "";

dobValue = getField("DOB").value;

if (dobValue!="") {

dob = util.scand("mm.dd.yyyy", dobValue);

now = new Date();

age = now.getFullYear() - dob.getFullYear();

if (now.getMonth() < dob.getMonth())

age--;

else if (now.getMonth()==dob.getMonth() && now.getDate() < dob.getDate())

age--;

event.value = age;

}

var dobValue = getField("DOB").value;
if (dobValue!="") {
var dob = util.scand("m/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);
var nMonthsLeft = Math.floor((nAgeYears - event.value) * 12);
}
else
$.rawValue = null
endif

This topic has been closed for replies.
Correct answer bebarth

Hi,

Please have a look on this post:

https://community.adobe.com/t5/acrobat-discussions/age-calculation-based-upon-date-of-birth-and-date-of-death-in-years-months-and-days/m-p/12093171#M315538

It will certainly help you to find the answer.

Let me know.

@+

2 replies

try67
Community Expert
Community Expert
July 25, 2022

This part of the code is not valid Acrobat JS syntax:

 

$.rawValue = null
endif

 

Is this an Acrobat form, or an LCD form?

Participating Frequently
July 25, 2022

Whoops I didn't even realize I pasted all that. I've been trying to figure this out all day and must've copied and pasted some of my notes. I found some LD scripts and was trying to get ideas. I'm using Adobe Acrobat DC. Below is what I'm using for years:

 

var dobValue = getField("DOB").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);
}

bebarth
Community Expert
bebarthCommunity ExpertCorrect answer
Community Expert
July 25, 2022
Participating Frequently
July 25, 2022

Thank you SO much!!!! I did see that one. The only part I think I am confused about is if I change the field names to match my form, where in the code do I change the field name so the code still works? I feel like I'm missing something?

bebarth
Community Expert
Community Expert
July 25, 2022

can you share your file?

@+