Copy link to clipboard
Copied
OK, so I'm trying to use a custom calculation script, tied to my AGE field to calculate age from the DOB field. The format of the AGE field is set to None and the format of the DOB field is set to Date, mm/dd/yyyy. This is the script I was working with and which I thought was working, but I see now it doesn't work for me when the DOB is 05/30/1926. Can someone please correct my script so it works and automatically updates the AGE field every time the PDF is opened, printed, or saved?
Here is my current script:
event.value = "";
dobValue = getField("DOB").value;
if (dobValue!="") {
dob = util.scand("dd/mm/yyyy", dobValue);
today = new Date();
// compute using year
event.value = today.getFullYear() - dob.getFullYear();
// adjust when today's month is before dob's month
event.value = event.value - (today.getMonth() < dob.getMonth() ) * 1;
// adjust when months are equal and date is before dob's date
event.value -= ( (today.getMonth() == dob.getMonth() ) && (today.getDate() < dob.getDate() ) ) * 1
}
I realize there are already several posts out there about this, but I've not found anything yet that resolves my issue and would love the help making this work! Thanks in advance for your time and attention!
Copy link to clipboard
Copied
How do you want to display the Age? in years only? or years,months, and days?
The calculation above is a bit over complicated. You might be better served by performing the calculation in the raw time value
dobValue = getField("DOB").value;
if (dobValue!="")
{
dob = util.scand("dd/mm/yyyy", dobValue);
var nTimeDiff = (new Date()).getTime() - dob.getTime();
var nYears = nTimeDiff/(365*24*60*60*1000);
event.value = nYears;
}
From here you can clean up the display value
Copy link to clipboard
Copied
The problem appears to have issues with dates after May 12, of any year, so it is not just the one date. Changing the format of the date to "dd mmm yyyy" resolves the issue or changing the dobValue to a string by using the field's valueAsString or the String constrictor,
I also use some document level functions for the getField and uti.scand methods to incorporate some error checking.
document level functions:
function GetField(oDoc, cName, bMsg)
{
if(typeof bMsg == "undefined")
{
var bMsg = false;
}
var oField = oDoc.getField(cName);
if(oField == null && bMsg == true)
{
app.alert("Error accessing field " + cName, 0, 1, "Get Field Error");
}
return oField;
}
function Scand(cFormat, cString)
{
var oDate = util.scand(cFormat, cString);
if(oDate == null)
{
app.alert("Error converting date time string " + cString + " with format " + cFormat, 0, 1, "Date Time Converstion Error");
}
return oDate;
}
Custom calculation script:
var dobField = GetField(this, "DOB");
var dobValue = dobField.valueAsString;
if (dobValue != "")
{
var dobFormat = "mm/dd/yyyy";
var dob = Scand("mm/dd/yyyy", dobValue);
var nTimeDiff = (new Date()).getTime() - dob.getTime();
var nYears = nTimeDiff /(365*24*60*60*1000);
event.value = Math.floor(nYears);
}
Copy link to clipboard
Copied
You mean the error is in the date conversion? Interesting.
Here is a version that rounds with the Math function to get a correct age in whole years
dobValue = getField("DOB").value;
if (dobValue!="")
{
dob = util.scand("dd/mm/yyyy", dobValue);
var nTimeDiff = (new Date()).getTime() - dob.getTime();
var nYears = nTimeDiff/(365*24*60*60*1000);
event.value = Math.round(nYears);
}
Copy link to clipboard
Copied
Also the number of days in a year should be 365.2425 to allow for leap years. I am not sure that one wants to use the round approach. If one enters 12/01/1926 using round results in an age of 91, but the individual is still considered 90 for most legal purposes.
Copy link to clipboard
Copied
So in reviewing all of this, I'd like to say, yes, I would like to account for leap years and I don't want to round results as I need it to be as accurate as possible. In your example, the correct age value should be 90 so whatever would account for that and provide that result is what I am looking for here. Also, it needs to update automatically; I don't want to have to select the field or something else manually to force an update; whenever the PDF file is opened, saved, or printed, it should always have the most current data in it for AGE. Can you or someone provide script to address all of this?
Copy link to clipboard
Copied
Here's the most recent code being used:
event.value = "";
var dobValue = getField("DOB").value;
if (dobValue!="") {
var dob = util.scand("mm/dd/yyyy", dobValue);
var today = new Date();
// compute years on full year only
var age = today.getFullYear() - dob.getFullYear();
// adjust for months before month of birth
if (today.getMonth() < dob.getMonth()) age = age - 1;
// adjust for today's month equal to dob's month and today's date before the date of birth
if ((today.getMonth() == dob.getMonth()) && (today.getDate() < dob.getDate())) age -= 1;
event.value = age;
}
Copy link to clipboard
Copied
That should work,
If you are getting an error or wrong answer what is the error or wrong value and what are you expecting?
If you have other calculations in the form, it is possible that there is an error in one of those calculations that is causing the rest of the calculations in form from working properly.
A link to a sample form with the error could be helpful.
Copy link to clipboard
Copied
Wow, glad I found this. Just used it and it works perfectly! Too much for my brain to have figured out.
Copy link to clipboard
Copied
Tricky, you have this down. I forgot about leap years, but unless you are getting into details, or the age is over 300 some years, I'd think this would work.
I see what you mean by rounding, legal age is based on the Floor of the year value.
Copy link to clipboard
Copied
So all of this is extremely helpful but I have no idea where I would put any of these scripts. Does it go in calculate? Format? Some other place? Please help!
Copy link to clipboard
Copied
It's a calculation script.
Copy link to clipboard
Copied
Could you step by step explain how I would go about inputting this into my form?
Copy link to clipboard
Copied
See this article for detailed instructions:
https://www.pdfscripting.com/public/Entering-Calculation-Scripts.cfm
Copy link to clipboard
Copied
You can leave me out on this one