Skip to main content
New Participant
November 30, 2017
Question

Calcuate AGE from DOB, Update Age Automatically

  • November 30, 2017
  • 2 replies
  • 9524 views

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!

This topic has been closed for replies.

2 replies

New Participant
January 19, 2020

You can leave me out on this one

Thom Parker
Community Expert
November 30, 2017

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

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
November 30, 2017

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

}

Thom Parker
Community Expert
November 30, 2017

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

}

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often