These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

AGE Calculation Help!

Tomme
Registered: Jun 2 2011
Posts: 11

I have read post after post after post and can still not get an AGE to calculate when a date is placed in a field. I am EXTREMELY new to this and feel defeated already. I know there are a lot of posts about this, but I am still not grasping it.
I need the age to go into a field titled "AGE" that is calculated from a date of birth field titled "DOB"
I have the current date being automatically filled in when the PDF opens in a field titled "DOS3"
 
I need some help! I am just a fireman, this is FAR above my pay grade! I am using Adobe Professional 9 (I think).

Tomme
Registered: Jun 2 2011
Posts: 11
//Here is what I have in the field
var fSec = 1000;
var fMin = 60 * fSec;
var fHr = 60 * fMin;
var fDay = 24 * fHr;
var fYear = 365.25 * fDay;
var strStart = this.getField("AGE").value;
var strToday = util.printd("mm/dd/yyyy", new Date());
var strToday = this.getField("DOB").value;
if(strStart.length & strToday.length) {
var DOB = util.scand("mm/dd/yyyy", strStart);
var Today = util.scand("mm/dd/yyyy", strToday);
var diff = Today.valueOf() - DOB.valueOf();
diff = diff + fDay;
event.value = Math.floor(diff/ fYear);
}

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Are you getting any errors in the Acrobat JavaScript debugging console?

What results are you getting?

I see that the variable 'strToday' is defined twice. The second definition overrides the previous value.

I find the following works:

// custom calculation for the Age field
// define some time constants
var fSec = 1000; // second in milliseconds
var fMin = 60 * fSec; // minute in millisecond
var fHr = 60 * fMin; // hour in milliseconds
var fDay = 24 * fHr; // day in milliseconds
var fYear = 365.25 * fDay; // year in milliseconds

// get the DOB field value
var strStart = this.getField("DOB").value;
// get the date of the incident
var strEnd = this.getField("Today").value

// with a DOB value compute age
if(strStart.length & strEnd.length) {
// get DOB as a date object
var oDOB = util.scand("mm/dd/yyyy", strStart);
// today's date as a date object
var oToday = util.scand("mm/dd/yyyy", strEnd)
// compute the difference between today and DOB values
var diff = oToday.valueOf() - oDOB.valueOf();
// add today to result
diff = diff + fDay;
// truncate to full years
event.value = Math.floor(diff/ fYear);
}

If this is a form that will be opened after it is originally completed, you should be computing the age from the date of the incident and not today's date as today's date will change every 24 hours.

George Kaiser

Tomme
Registered: Jun 2 2011
Posts: 11
double post, sorry
Tomme
Registered: Jun 2 2011
Posts: 11
The Field titled "dos3" is the Date that is autopopulated when the form is opened. It is always the current date.

The field titled "dob" is where the patients date of birth goes. Here is what I have tried and STILL doesnt work:

// custom calculation for the Age field
// define some time constants
var fSec = 1000; // second in milliseconds
var fMin = 60 * fSec; // minute in millisecond
var fHr = 60 * fMin; // hour in milliseconds
var fDay = 24 * fHr; // day in milliseconds
var fYear = 365.25 * fDay; // year in milliseconds

// get the DOB field value
var strStart = this.getField("dob1").value;
// get the date of the incident
var strEnd = this.getField("dos3").value

// with a DOB value compute age
if(strStart.length & strEnd.length) {
// get DOB as a date object
var odob1 = util.scand("mm/dd/yyyy", strStart);
// today's date as a date object
var odos3 = util.scand("mm/dd/yyyy", strEnd)
// compute the difference between today and DOB values
var diff = odos3.valueOf() - odob1.valueOf();
// add today to result
diff = diff + fDay;
// truncate to full years
event.value = Math.floor(diff/ fYear);
}

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Are you getting any errors in the JavaScript console?

George Kaiser

Tomme
Registered: Jun 2 2011
Posts: 11
TO be honest, I am not even sure what that is.
When I go to the properties of the field where I want the AGE to display and go to ACTIONS and choose MOUSE UP and RUN A JAVASCRIPT the javascript editor comes up. The above is what I have placed in the editor. There are no errors noted.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You need to place it under Calculate, Custom calculation script, not as a MouseUp action.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com