Copy link to clipboard
Copied
Full disclaimer: I know NOTHING about Javascript. I've copied and pasted the scripts of other geniuses and have tried to understand the logic behind their scripts but just can't seem to grasp it. Anyway, I have an age field on a form into which I want the age in years automatically calculated from the DOB and the document's date based on separate fields for month, day and year. Until now, I could only find the following script that calculates based on the current date. I don't want the current date but, instead, the date typed into three separate date fields for Month / Day / Year (fields are "MM", "DD" and "Year"). The current script is below:
var dob = util.scand("mm/dd/yyyy", this.getField("Date of Birth").valueAsString);
if (dob != null) {
var today = new Date();
var age = today.getFullYear() - dob.getFullYear();
var m = today.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
age--;
}
event.value = age;
}
else {
event.value = "";
}
if(event.value == 0)
event.value = "";
So I figured it out using your recommendation and it worked! However, now I don't know how to keep the age blank if no dates are typed in. This is where I'm at:
var docString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;
var dob = util.scand("mm/dd/yyyy", this.getField("Date of Birth").valueAsString);
if (dob != null) {
var today = util.scand("mm/dd/yyyy", docString);
var age = today.getFullYear() - dob.getFullYear();
var m
...If any of the fields is empty the dob variable will be null and the field will be empty.
Copy link to clipboard
Copied
Replace the first line of the code with this:
var dobString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;
var dob = util.scand("mm/dd/yyyy", dobString);
Copy link to clipboard
Copied
Thank you so much try67. I tried it but nothing calculated. The age box remains blank. Just using logical assumptions, I tried this and it also didn't work:
var docString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;
var dob = util.scand("mm/dd/yyyy", docString);
if (dob != null) {
var today = docString();
var age = today.getFullYear() - dob.getFullYear();
var m = today.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
age--;
}
event.value = age;
}
else {
event.value = "";
}
I don't know where to go from here or what else to try. By the way, I don't think I mentioned that I'm using Adobe Acrobat Pro XI on Windows 10.
Copy link to clipboard
Copied
If there are no error messages in the JS Console then share the file with us for further help with it.
Copy link to clipboard
Copied
Following line is not correct:
var today = docString();
docString is not a function.
Copy link to clipboard
Copied
Oh, wait. I now see that I wasn't clear in my OP. The dob field is one text field. The document date field is what is separated as MM, DD, Year. So I want the age to be the difference in years between the dob (already in mm/dd/yyyy format in one field) and the document's date (which is in three separate fields: MM, DD, Year). Currently it is grabbing the current date.
Copy link to clipboard
Copied
So I figured it out using your recommendation and it worked! However, now I don't know how to keep the age blank if no dates are typed in. This is where I'm at:
var docString = this.getField("MM").valueAsString + "/" + this.getField("DD").valueAsString + "/" + this.getField("Year").valueAsString;
var dob = util.scand("mm/dd/yyyy", this.getField("Date of Birth").valueAsString);
if (dob != null) {
var today = util.scand("mm/dd/yyyy", docString);
var age = today.getFullYear() - dob.getFullYear();
var m = today.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
age--;
}
event.value = age;
}
else {
event.value = "";
}
if(event.value == 0)
event.value = "";
Thanks!
Copy link to clipboard
Copied
If any of the fields is empty the dob variable will be null and the field will be empty.
Copy link to clipboard
Copied
Hi there!
Could someone please offer reference as to where this script should be entered? I have tried properties>calculation>custom..., but nothing populates in the pdf preview. The date of birth entered will be one string, so do I need to add the first MM, DD, YEAR portion to the script? Also, do I need to do anything to the cell in which the user will input the DOB, or just make sure its title is correct in the script?
Copy link to clipboard
Copied
You've already posted in another thread, so let's continue there. There's no need to ask in multiple places.