6 Replies Latest reply on Aug 31, 2015 3:18 AM by Paulinafrica

    Calculate years (age) between two dates


      Good day!


      I haven't been able to find an answer I've been able to use in the archives or online, and would very much appreciate some help.  I am working with Adobe Acrobat Pro XI (not LiveCycle), and have very limited programming experience (mostly copy / paste).


      I need to create a text box that calculates the number of years between two entered dates, i.e. age when date of birth and date of death are given.  It would also be nice if it were blank until both were provided.  Would someone be able to give me the code I'd need to put in the custom calculation script?


      Again, any help would be much appreciated!  Thanks and have a wonderful day!

        • 1. Re: Calculate years (age) between two dates
          gkaiseril MVP & Adobe Community Professional

          For the age in full years and the fields DOB for Date of Birth and DOD for Date of Death both using a date format of "d-mmm-yyyy" you can use a custom JavaScript calculation for the age field like:


          function GetField(cName) {
          // get field object with erro trapping;
          var oField = this.getField(cName);
          // test for error;
          if(oField == null) app.alert("Error accessing " + cName, 1, 0);
          // return field object;
          return oField;
          } // end GetField function;


          function Scand(cFormat, cString) {
          // using format convert date string  to date object;
          // convert date string using format;
          var oDate = util.scand(cFormat, cString);
          // null value is a conversion error;
          if(oDate == null) app.alert("Error converting " + cString + "using format " + cFormat, 1, 0);
          // return date object;
          return oDate;
          } // end Scand function;


          // clear the field value;

          event.value ="";
          // get dob and dod data;
          var oDOB = GetField("DOB");
          var oDOD = GetField("DOD");

          // process only if DOB and DOD have a value and are not null;
          if(oDOB != null && oDOD != null && oDOB.value != "" && oDOD.value != "") {

          // convert DOB and DOD to date objects;
          var oDOB = util.scand("d-mmm-yyyy", oDOB.value);
          var oDOD = util.scand("d-mmm-yyyy", oDOD.value);

          // get difference in years;
          var Age = oDOD.getFullYear() - oDOB.getFullYear();

          // adjust for DOD month before DOB month;
          if(oDOD.getMonth() < oDOB.getMonth()) Age--;

          // adjust for DOD date before DOB date in month of birth;

          if(oDOD.getMonth() == oDOB.getMonth() && oDOD.getDate() < oDOB.getDate()) Age--;
          // set the field value;

          event.value = Age;

          • 2. Re: Calculate years (age) between two dates
            an3ph Level 1

            Thank you, GK!  You rock!  This seems to work like a charm!  I very much appreciate your help!

            • 3. Re: Calculate years (age) between two dates

              Thank you! Just what I was looking for. Your instructions and code was very easy to implement

              • 4. Re: Calculate years (age) between two dates

                Hello GK

                Thanks for the script, well deserving of your MVP status, that has helped me immensely!


                My next task in life is to further my education with JavaScript.


                OK, I have picked up one anomaly, when I save the form as Reader Extended pdf > Enable Additional Features the script does not appear to be working any longer.


                It works fine prior to saving, any clues?



                • 5. Re: Calculate years (age) between two dates
                  gkaiseril MVP & Adobe Community Professional

                  Make sure you saved the form with the Extended Rights to include Form Filling and Saving.

                  • 6. Re: Calculate years (age) between two dates
                    Paulinafrica Level 1

                    Hello GK


                    Thanks for taking the time to reply.


                    The options available to me under Acrobat Pro X Reader Extended PDF are:


                        Enable Adding Text in Documents (that are not fillable forms)...

                        Enable Commenting & Measuring...

                        Enable Additional Features...



                    Clearly the first 2 are not going to work and I have already tried the third option without success, I will retry again this morning.


                    OK, I have retried it again this morning and it seems to be working as expected, not sure why it wasn't working on Friday!


                    We did have some network issues on Friday, although I can't see why that would make any difference, today I did change the name of the field with the calculation, to a name with no spaces.


                    Grasping at straws here!