8 Replies Latest reply on Nov 30, 2017 12:31 PM by gkaiseril

    Calcuate AGE from DOB, Update Age Automatically

    mhc6332847

      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!

        • 1. Re: Calcuate AGE from DOB, Update Age Automatically
          Thom Parker Adobe Community Professional

          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

          • 2. Re: Calcuate AGE from DOB, Update Age Automatically
            gkaiseril MVP & Adobe Community Professional

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

            }

            • 3. Re: Calcuate AGE from DOB, Update Age Automatically
              Thom Parker Adobe Community Professional

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

              }

              • 4. Re: Calcuate AGE from DOB, Update Age Automatically
                gkaiseril MVP & Adobe Community Professional

                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.

                • 5. Re: Calcuate AGE from DOB, Update Age Automatically
                  mhc6332847 Level 1

                  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?

                  • 6. Re: Calcuate AGE from DOB, Update Age Automatically
                    mhc6332847 Level 1

                    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;

                     

                    }

                    • 7. Re: Calcuate AGE from DOB, Update Age Automatically
                      Thom Parker Adobe Community Professional

                      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.

                      • 8. Re: Calcuate AGE from DOB, Update Age Automatically
                        gkaiseril MVP & Adobe Community Professional

                        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.