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

Calculate Age in Years AND Months

Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

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

1.1K

Translate

Translate

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 guidelines

correct answers 1 Correct answer

Community Expert , Jul 25, 2022 Jul 25, 2022

Votes

Translate

Translate
Community Expert ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

Copied

Hi,

Please have a look on this post:

https://community.adobe.com/t5/acrobat-discussions/age-calculation-based-upon-date-of-birth-and-date...

It will certainly help you to find the answer.

Let me know.

@+

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

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?

Votes

Translate

Translate

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 guidelines
Community Expert ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

Copied

can you share your file?

@+

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

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!

Votes

Translate

Translate

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 guidelines
Community Expert ,
Jul 26, 2022 Jul 26, 2022

Copy link to clipboard

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!

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Jul 26, 2022 Jul 26, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Jul 26, 2022 Jul 26, 2022

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

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 guidelines
Community Expert ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

Copied

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

 

$.rawValue = null
endif

 

Is this an Acrobat form, or an LCD form?

Votes

Translate

Translate

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 guidelines
Community Beginner ,
Jul 25, 2022 Jul 25, 2022

Copy link to clipboard

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

Votes

Translate

Translate

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 guidelines