• Calculate Age in Years AND Months

# Calculate Age in Years AND Months

Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copied

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

TOPICS
JavaScript

Views

75

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Enthusiast , Jul 25, 2022 Jul 25, 2022

Likes

9 Replies 9
Enthusiast ,
Jul 25, 2022 Jul 25, 2022

Copied

Hi,

Please have a look on this post:

Let me know.

@+

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copied

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?

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Enthusiast ,
Jul 25, 2022 Jul 25, 2022

Copied

@+

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copied

I can't share as it's work related but I may have realized where I went wrong (since I copied and pasted the completely wrong script above). I will look tomorrow and see and let you know. Fingers crossed. Thank you!

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 26, 2022 Jul 26, 2022

Copied

The only thing you have to adjust in the code (assuming it's working correctly) is the text in quotes in this line:

getField("DOB").value

That text needs to perfectly match the name of your Date of Birth field, including upper/lower-case and spaces!

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 26, 2022 Jul 26, 2022

Copied

Yes, I had some mix ups. Your script worked PERFECTLY! You saved the day! Thank you!!!

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 26, 2022 Jul 26, 2022

Copied

LATEST

Any chance you know how I can have the years and months reset when the DOB is blank? Thank you again 🙂

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 25, 2022 Jul 25, 2022

Copied

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

\$.rawValue = null
endif

Is this an Acrobat form, or an LCD form?

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copied

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);
}

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Resources