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
Hi,
Please have a look on this post:
It will certainly help you to find the answer.
Let me know.
@+
Copy link to clipboard
Copied
Hi,
Please have a look on this post:
It will certainly help you to find the answer.
Let me know.
@+
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?
Copy link to clipboard
Copied
can you share your file?
@+
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!
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!
Copy link to clipboard
Copied
Yes, I had some mix ups. Your script worked PERFECTLY! You saved the day! Thank you!!!
Copy link to clipboard
Copied
Any chance you know how I can have the years and months reset when the DOB is blank? Thank you again 🙂
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?
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);
}