13 Replies Latest reply on Sep 5, 2017 1:46 PM by gkaiseril

    Calculating Age from DOB field in Acrobat

    jagan18

      Hello everyone,

       

      I am placing this JavaScript code below in the calculate tab in the custom calculation script window for an Adobe acrobat form I am creating.  The user will enter in the DOB field "mm/dd/year" and the age output should be in the "age" field.  For the moment the code works up to the the day 12th day after that the age does not show up in the age field and if I put a date of 1/16/1973 my age should be 39, but my age comes out as 38.  Can anyone tell me what I am missing in the code?  All so its not accounting for the leap year.   Any help will be appreciated.  Thanks!

       

      event.value = "";

      dobValue = getField("dob").value;

      if (dobValue!="") {

      dob = util.scand("dd/mm/yyyy", dobValue);

      today = new Date();

      age = today.getFullYear() - dob.getFullYear();

      if (today.getMonth() < dob.getMonth())

      age;

      else if (today.getMonth()== dob.getMonth())

      age;

      else if (today.getDate() < dob.getDate())

      age;

      else if (today.getDate() == dob.getDate())

      age;

      event.value = age;

      }

        • 1. Re: Calculating Age from DOB field in Acrobat
          gkaiseril MVP & Adobe Community Professional

          You subtract the years to get a tentative age in years. Then you check do some checks on the month but it does not appear you are adjusting the age on the month comparison and the same for the date.

           

          Have yo thought about a straight computation of the age?

           

          event.value = "";

          var dobValue = getField("dob").value;

          if (dobValue!="") {

          var dob = util.scand("dd/mm/yyyy", dobValue);

          var today = new Date();

          // compute age in milliseconds

          var age = today.getTime() - dob.getTime();

          // convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

          // truncate to whole years and adjust for binary floating point error

          event.value = Math.floor( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

          }

           

          Or you need to adjust the years for months after the dob month and when the month is equal to the dob month the dates after the dob date.

           

          event.value = "";

          var dobValue = getField("dob").value;

          if (dobValue!="") {

          var dob = util.scand("dd/mm/yyyy", dobValue);

          var today = new Date();

          // compute years on full year only

          var age = today.getFullYear() - dob.getFullYear();

          console.show();

          console.clear();

          console.println("today year: " + today.getFullYear() + " Dob full year: " + dob.getFullYear());

          console.println("today month: " + today.getMonth()  + " DOB month: " +  dob.getMonth());

          // 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

          console.println("today date: " + today.getDate() + " DOB date: " + dob.getDate())

          if ((today.getMonth() == dob.getMonth()) && (today.getDate() < dob.getDate())) age -= 1;

          event.value = age;

          }

           

          Or get tricky and use no if statements for the adjustments:

           

          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 moths are equal and date is before dob's date

          event.value -= ( (today.getMonth() == dob.getMonth() ) && (today.getDate() < dob.getDate() ) ) * 1

          }

           

          The logical statement is 1 for true and 0 for false.

          1 person found this helpful
          • 2. Re: Calculating Age from DOB field in Acrobat
            jagan18 Level 1

            Thank you your scripts wotk great!!!

            • 3. Re: Calculating Age from DOB field in Acrobat
              g5reality

              I am attempting to add the above script to my employee census form. It works for the field "DOB  AGERow1" but when you enter the DOB in field "DOB  AGERow2" the calculated age in row 1 disapears? When field "DOB  AGERow3" is calculated correctly age in row 2 disapears.

               

              Any suggestions for multiple DOB age calculations on one form?

               

              What am I missing?

               

              I am using this script

               

              event.value = "";

              var dobValue = getField("dob").value;

              if (dobValue!="") {

              var dob = util.scand("dd/mm/yyyy", dobValue);

              var today = new Date();

              // compute age in milliseconds

              var age = today.getTime() - dob.getTime();

              // convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

              // truncate to whole years and adjust for binary floating point error

              event.value = Math.floor( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

              }

              • 4. Re: Calculating Age from DOB field in Acrobat
                gkaiseril MVP & Adobe Community Professional

                The script is for the custom calculation option of a field.

                 

                You will need to update the field name for each age field to access the correct DOB source field.

                 

                Have you opened the JavaScript console to look for errors. I would expect you are getting a "field not found" error.

                 

                var RowIndex = 1; // change for row number

                event.value = "";

                try {

                var dobValue = getField("DOB AGERow" + RowIncex).value;

                if (dobValue!="") {

                var dob = util.scand("dd/mm/yyyy", dobValue);

                var today = new Date();

                // compute age in milliseconds

                var age = today.getTime() - dob.getTime();

                // convert age to years ( millsec in sec * sec in min * min in hrs * hrs in day * days in year)

                // truncate to whole years and adjust for binary floating point error

                event.value = Math.floor( age / (1000 * 60 * 60 * 24 * 365.2425) - 0.005);

                }

                } catch(e) {

                app.alert("An error has occured!\n" + e.toString(), 0, 0);

                }

                • 5. Re: Calculating Age from DOB field in Acrobat
                  g5reality Level 1

                  Thanks, The error I now get is:

                   

                  "RowIncex not defined"

                   

                  Please advise

                   

                  Thanks

                  • 6. Re: Calculating Age from DOB field in Acrobat
                    gkaiseril MVP & Adobe Community Professional

                    This line:

                    var dobValue = getField("DOB AGERow" + RowIncex).value;

                     

                    Should be:

                    var dobValue = getField("DOB AGERow" + RowIndex).value;

                    • 7. Re: Calculating Age from DOB field in Acrobat
                      BrentVogel Level 1

                      I have little to NO experience with this type of thing.  "closer to NO"

                      but i tried putting your scripts into a form that i am also creating and it doesn't do anything.

                       

                      Any ideas what i am doing wrong

                       

                      I have a field for dob and age.

                       

                      under the properties of the age field/calculate/custom calculation script:

                       

                      I tried all of your scripts and nothing seems to work.  When i enter a Date of Birth on the dob field the age fields just remains blank.

                       

                      I'm sure it is something stupid that i am missing. but could use some help

                      • 8. Re: Calculating Age from DOB field in Acrobat
                        treborsky12

                        Your script works very well.

                         

                        I have a form which must calculate the age from a set "date" field rather than from "today".

                        Removing the today = Date() statement yields a blank result.

                        Can someone offer help in completing this calculation?

                        Thanks for any assistance.

                         

                        event.value = "";

                        dobValue = getField("Birth Date").value;

                        todayValue = getField ("Date").value;

                        if (dobValue!="") {

                        dob = util.scand("dd/mm/yyyy", dobValue);

                        today = util.scand("dd/mm/yyyy", todayValue);

                        today = 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 moths are equal and date is before dob's date

                        event.value -= ( (today.getMonth() == dob.getMonth() ) && (today.getDate() < dob.getDate() ) ) * 1

                        }

                        • 9. Re: Calculating Age from DOB field in Acrobat
                          gkaiseril MVP & Adobe Community Professional

                          Have you thought about the difference in how the age is computed from today's date and given date?

                           

                          The difference is that instead of today's date one is using given date. You need to replace getting today's field value with the fixed date's value as a string with the correct formatting..

                          • 10. Re: Calculating Age from DOB field in Acrobat
                            gkaiseril MVP & Adobe Community Professional

                            A  possible script is:

                             

                            // common functions of reused code;
                            function GetField(cName) {
                            // get field object
                            var oField = this.getField(cName);
                            // test for existance of field;
                            if (oField == null) {
                            app.alert("Field " + cName + " not found.\nPlease check spelling and capitalization of the field name.", 0, 1);
                            }
                            // return field object;
                            return oField;
                            } // end GetField function;

                            function Scand(cFormat, cDate) {
                            var oDate = util.scand(cFormat, cDate);
                            // test for valid date conversion;
                            if(oDate ==  null) {
                            app.alert("Error converting " + cDate + " with format " + cFormat + "\nConversion failed.", 0, 1)
                            }
                            // return date object;
                            return oDate;
                            } // end Scand function;
                            // end common functions reused code;

                            // custom calculation date;
                            var cBirthDate = "Birth Date"; // date of birth field name;
                            var cCutoffDate = "Cutoff Date"; // date field to use for cutoff date;
                            var cDateFormat = "dd/mm/yyyy"; // format for date fields;
                            event.value = "";
                            var cDob = GetField(cBirthDate).value;
                            var cCutoff = GetField(cCutoffDate).value;
                            if (cDob != "" && cCutoff != "") {
                            var oDob = Scand(cDateFormat, cDob);
                            var oCutoff = Scand(cDateFormat, cCutoff);
                            // compute using only year
                            event.value = oCutoff.getFullYear() - oDob.getFullYear();
                            // adjust when today's month is before dob's month
                            event.value = event.value -  Number(oCutoff.getMonth() < oDob.getMonth());
                            // adjust when moths are equal and date is before dob's date
                            event.value -= Number((oCutoff.getMonth() == oDob.getMonth())) && Number(oCutoff.getDate() < oDob.getDate());
                            }
                            // end custom calculation date;

                             

                            You need to change field names as necessary.

                            1 person found this helpful
                            • 11. Re: Calculating Age from DOB field in Acrobat
                              michaelh51562788

                              What if i wanted the final output of the Age calculation to publish the years and months to look like this [ 3;6 ] so 3 years and 6 months basically. This is the script that i used. it works great i just need the months to show as well. Thank you

                               

                              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 moths are equal and date is before dob's date

                              event.value -= ( (today.getMonth() == dob.getMonth() ) && (today.getDate() < dob.getDate() ) ) * 1

                              }

                              • 12. Re: Calculating Age from DOB field in Acrobat
                                nyftdoc

                                So I have used the below calculation.  It works Great.  Using the calculation how do I get it to display #years/#months.  in the age box.

                                 

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

                                console.show();

                                console.clear();

                                console.println("today year: " + today.getFullYear() + " Dob full year: " + dob.getFullYear());

                                console.println("today month: " + today.getMonth()  + " DOB month: " +  dob.getMonth());

                                // 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

                                console.println("today date: " + today.getDate() + " DOB date: " + dob.getDate())

                                if ((today.getMonth() == dob.getMonth()) && (today.getDate() < dob.getDate())) age -= 1;

                                event.value = age;

                                }

                                • 13. Re: Calculating Age from DOB field in Acrobat
                                  gkaiseril MVP & Adobe Community Professional

                                  Try:

                                   

                                  function getAge(fromdate, todate){
                                      if(todate) todate= new Date(todate);
                                      else todate= new Date();

                                      var age= [], fromdate= new Date(fromdate),
                                      y= [todate.getFullYear(), fromdate.getFullYear()],
                                      ydiff= y[0]-y[1],
                                      m= [todate.getMonth(), fromdate.getMonth()],
                                      mdiff= m[0]-m[1],
                                      d= [todate.getDate(), fromdate.getDate()],
                                      ddiff= d[0]-d[1];

                                      if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff;
                                      if(mdiff<0) mdiff+= 11;
                                      if(ddiff<0){
                                          fromdate.setMonth(m[1]+1, 0);
                                          ddiff= fromdate.getDate()-d[1]+d[0];
                                          --mdiff;
                                      }
                                      if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' '));
                                      if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':''));
                                  //    if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':''));
                                      if(age.length>1) age.splice(age.length-1,0,' and ');
                                      return age.join('');
                                  }

                                   

                                  event.value = ""; // clear result;
                                  var cDob = this.getField("dob").value;
                                  if(cDob != "") {

                                  var today = new Date();
                                  var dob = util.scand("mm/dd/yyyy", cDob);
                                  if(today.getTime() > dob.getTime()) {
                                     event.value = getAge(util.printd("yyyy,mm,dd", dob), util.printd("yyyy,mm,dd", today));
                                    } else {
                                     app.alert("Date of birth can not be after today!" , 1, 0, "Dob After Today");
                                    } // end if dob before today;
                                  } // end dob not empty;