1 2 Previous Next 42 Replies Latest reply: Nov 11, 2014 8:46 AM by GKaiseril RSS

    Age field based on date field

    WillCD Community Member

      How do I create a field which populates an age based on another date field?

       

      Adobe Acrobat 9

        • 1. Re: Age field based on date field
          GKaiseril CommunityMVP

          You have to use JavaScript to create the custom calculation script to get the desired result.

           

           

          For example in decimal years:

           

          function Date2Num(cFormat, cDate) {
          /*
          convert date string to number of days since Epoch date
          Inputs:
          cDate - date sting
          cFormat - format of date sting
          Returns:
          number of dasy since Epoc date.
          */
          // convert date string to date object
          var oDate = util.scand(cFormat, cDate);
          // convert milliseconds from Epoch date to whole days
          return Math.floor(oDate.getTime() / (1000 * 60 * 60 * 24));
          } // end of Date2Num

          event.value = "";
          // get dob string value
          var sDob = this.getField("DOB").value;
          if(sDob != "") {
          // compute is dob is not empty
          var cToday = util.printd( "dd-mmm-yyyy", (new Date()))
          // compute difference includig end date
          var nDiff = Date2Num(cToday, "dd-mmm-yyyy", cToday) - Date2Num("dd-mmm-yyyy", sDob);
          // compute output in years with decimals
          event.value =(nDiff / 365.2425) + " years old";
          }

           

           

          Years months & days:

           

          event.value = "";

          // get dob string value

          var sDob = this.getField("DOB").value;

          if(sDob != "") {

          // convert to date object

          var oDob = util.scand("dd-mmm-yyyy", sDob);

          // todays date in milliseconds since Epoch date

          var oToday = new Date()

          // compute output

          nYears = oToday.getFullYear() - oDob.getFullYear();

          nMonths = oToday.getMonth() - oDob.getMonth();

          nDays = oToday.getDate() - oDob.getDate();

          if(nDays < 0) {

          // table for months days

          // fix for days in month

          nMonths--;

          nDays = nDays + 30;

          }

          if(nMonths < 0) {

          nYears--;

          nMonths = nMonths + 12;

          }

          event.value = nYears + " years " + nMonths + "  months " + nDays + " days";

          }

           

          Whole years only:

           

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

          var TodayMMDD = today.getMonth() + today.getDate()

          var dobMMDD = dob.getMonth() + dob.getDate();

          if( TodayMMDD < dobMMDD) age -= 1;

          event.value = age;

          }

          • 2. Re: Age field based on date field
            WillCD Community Member

            This is way over my head. If I copy and paste this into my document (in the properties of the field), will it work?

            • 3. Re: Age field based on date field
              GKaiseril CommunityMVP

              Only if your field for he date of birth is named "DOB" in all caps.

              • 4. Re: Age field based on date field
                WillCD Community Member

                I changed the name of my Text Field (which contains the date of birth) from a102 to DOB. I created a new Text Field called Test and pasted:

                 

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

                var TodayMMDD = today.getMonth() + today.getDate()

                var dobMMDD = dob.getMonth() + dob.getDate();

                if( TodayMMDD < dobMMDD) age -= 1;

                event.value = age;

                }

                 

                ...into the Format tab under Custom and Custom Format Script. Is that right? It does not appear to have worked.

                • 5. Re: Age field based on date field
                  GKaiseril CommunityMVP

                  You are performing a calculation not a formatting action.

                   

                  You need to put the code in the Custom Calculation Script. You might want to carefully reread the post.

                  • 6. Re: Age field based on date field
                    WillCD Community Member

                    My mistake. I repasted in Custom Calculation Script and it still did not work. I noticed that in your code, you formatted the date with 2 digits for day, month and 4 digits for year. So I changed my DOB text field to 2 digits for day and month and 4 digits for year. Do those need to be the same?

                     

                    It still did not work, and then I noticed in your code that you have day first, then month, then year. So I edited the code and put mm/dd/yyy in place of dd/mm/yyyy. I retried and now my new Age text field populates an age when a date of birth is entered into the DOB field. But unfortunately, it was the wrong age! I entered 03/28/1983 into the DOB field and the Age field populated 29, instead of 30. Any thoughts why?

                    • 7. Re: Age field based on date field
                      WillCD Community Member

                      Also, I have 6 DOB fields and need 6 age calculations. What part(s) of the calculation code would I need to rename to have 6 different calculations and for the Age fields to correspond to the proper DOB fields? Thanks.

                      • 8. Re: Age field based on date field
                        GKaiseril CommunityMVP

                        The comparison for the adjustment of the whole years was going the wrong way.

                         

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

                        var TodayMMDD = today.getMonth() + today.getDate()

                        var dobMMDD = dob.getMonth() + dob.getDate();

                        if( TodayMMDD > dobMMDD) age -= 1;

                        event.value = age;

                        }

                        • 9. Re: Age field based on date field
                          WillCD Community Member

                          Okay, so I repasted the new code and now some birthdays work and others don't.

                           

                          Examples:

                          03/28/1983 correctly shows age 30.

                          03/24/1984 correctly shows age 29.

                          08/30/2008 incorrectly shows age 5. Should be 4.

                          01/16/2011 incorrectly shows age 1. Should be 2.

                           

                          Thoughts?

                          • 10. Re: Age field based on date field
                            WillCD Community Member

                            Does anyone know what I can do here? Does anyone know why the ages are wrong?

                            • 11. Re: Age field based on date field
                              Gilad D (try67) CommunityMVP

                              The script assumes the format of the dates is "dd/mm/yyyy", while you're using "mm/dd/yyyy". You need to change one of them...

                              • 12. Re: Age field based on date field
                                WillCD Community Member

                                I believe I have. Here is the script I'm using:

                                 

                                event.value = "";

                                var dobValue = getField("a104").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();

                                var TodayMMDD = today.getMonth() + today.getDate()

                                var dobMMDD = dob.getMonth() + dob.getDate();

                                if( TodayMMDD > dobMMDD) age -= 1;

                                event.value = age;

                                }

                                • 13. Re: Age field based on date field
                                  WillCD Community Member

                                  The format of the text field that this is pulling from is mm/dd/yyyy.

                                   

                                  I just checked and all seem to be a year off now.

                                   

                                  03/28/1983 showing 29, instead of 30. (This is 1 year too YOUNG.)

                                  03/24/1984 showing 28, instead of 29. (This is 1 year too YOUNG.)

                                  08/30/2008 showing 5, instead of 4. (This is 1 year too OLD.)

                                  01/16/2011 showing 1, instead of 2. (This is 1 year too YOUNG.)

                                   

                                  Any ideas why it's always off 1 year and why sometimes it's 1 year too old and sometimes it's 1 year too young?

                                  • 14. Re: Age field based on date field
                                    Gilad D (try67) CommunityMVP

                                    The script you were given is invalid. Use this instead:

                                     

                                    event.value = "";

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

                                    if (dobValue!="") {

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

                                        var today = new Date();

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

                                        if (today.getMonth()>dob.getMonth()) {

                                            age++;

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

                                            if (today.getDate()>dob.getDate())

                                                age++;

                                        }

                                        event.value = age;

                                    }

                                    • 15. Re: Age field based on date field
                                      WillCD Community Member

                                      This works. Thank you. I did find one problem which is not the end of the world. I put in 07/26/XXXX and it is 1 year too young. (That is today's date.) Is it one day behind when it comes to calculating on the same day as the birth day?

                                      • 16. Re: Age field based on date field
                                        Gilad D (try67) CommunityMVP

                                        It's just a question of when to make the change from one year to another. If you want to include the birthday itself in the next year, change this line in the code:

                                        if (today.getDate()>dob.getDate())

                                        To this:

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

                                        • 17. Re: Age field based on date field
                                          GKaiseril CommunityMVP

                                          Here is a working file for computing the Age from the DOB.

                                           

                                          Note the formatting for the DOB field must be the same for the DOB field and the script.

                                           

                                          Formatting of the entry is very important.

                                           

                                          event.value = "";

                                          // name for DOB field

                                          DOB_field_name = "DOB";

                                          // format for dob field

                                          var cDateFormat = "dd-mmm-yyyy";

                                          try {

                                          var dobValue = getField(DOB_field_name).value;

                                          } catch (e) {

                                          app.alert("Error getting value of field " + DOB_field_name, 0, 1);

                                          dobValue = "";

                                          } finally {

                                          if (dobValue!="") {

                                          var dob = util.scand(cDateFormat, dobValue);

                                          var today = new Date();

                                          // compute years on full year only

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

                                          // adjust for months after month of birth

                                          if (today.getMonth() < dob.getMonth()) age--;

                                          // adjust for today month equal to dob month and today's date after day of birth

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

                                          event.value = age;

                                          } // end dobValue not empty

                                          } // end get field value

                                          • 18. Re: Age field based on date field
                                            WillCD Community Member

                                            This fixed the birthday issue. Thank you.

                                            • 19. Re: Age field based on date field
                                              WillCD Community Member

                                              Hey Gilad D, I noticed that this no longer works on the pdf once they become older. It's as if it is calculating the field once a birthday is originally entered and then never calculates it again. Is there a way to have this be dynamic, so that when I open the pdf, it will show the correct age presently?

                                              • 20. Re: Age field based on date field
                                                Gilad D (try67) CommunityMVP

                                                Yes. You need to embed the code at the document level, so that it gets executed each time the file is opened. Basically the only thing you need to change in it is instead of accessing event.value you need to use this.getField("Age").value, or whatever is the name of the Age field in your file.

                                                • 21. Re: Age field based on date field
                                                  WillCD Community Member

                                                  Here is the code I currently have. What would you change?

                                                   

                                                  event.value = "";

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

                                                  if (dobValue!="") {

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

                                                      var today = new Date();

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

                                                      if (today.getMonth()>dob.getMonth()) {

                                                          age++;

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

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

                                                              age++;

                                                      }

                                                      event.value = age;

                                                  }

                                                  • 22. Re: Age field based on date field
                                                    Gilad D (try67) CommunityMVP

                                                    Exactly what I've described.

                                                    • 23. Re: Age field based on date field
                                                      GKaiseril CommunityMVP

                                                      Have you checked the JavaScript Console for errors?

                                                       

                                                      Can you post a link to a sample form?

                                                       

                                                      The form I linked to works and adjusts with each new entry.

                                                      • 24. Re: Age field based on date field
                                                        WillCD Community Member

                                                        Gilad D, here is what I have in the field. Does this look right? My Date of Birth field is a104 and my age field is a102. This does not seem to work. Yesterday (10/15/2014), I put the Date of Birth as 10/16/2013 and the age field populated a "0." That is correct. When I opened it up today, the age field should have populated a "1," but it did not. This is the problem I'm running into. Thoughts?

                                                         

                                                        this.getField("a102").value = "";

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

                                                        if (dobValue!="") {

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

                                                            var today = new Date();

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

                                                            if (today.getMonth()>dob.getMonth()) {

                                                                age++;

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

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

                                                                    age++;

                                                            }

                                                            this.getField("a102").value = age;

                                                        }

                                                        • 25. Re: Age field based on date field
                                                          WillCD Community Member

                                                          GKaiseril, I want to make sure you understand my dilemma. I need the age field to be dynamic and not static. The age field correctly populates when I enter in the Date of Birth, but the age field never changes, even when a year passes by and the age is now older. Does that make sense?

                                                           

                                                          Yesterday, I did a test and put in a DOB as 10/16/2013, which would mean the age yesterday was "0" and the age today would be "1." The pdf still says "0" today. Thoughts?

                                                          • 26. Re: Age field based on date field
                                                            GKaiseril CommunityMVP

                                                            Again, are you getting any errors in the JavaScript console?

                                                             

                                                            Again can you post a link to the form?

                                                             

                                                             

                                                            I would set the default value of the DOB field to the value of the field and then create a document level script to reset the DOB field.

                                                            • 27. Re: Age field based on date field
                                                              WillCD Community Member

                                                              GKaiseril, I'm not sure what the JavaScript console is. I can't post a link to the form unfortunately. I wish I could. Here's what happens:

                                                               

                                                              I set the DOB and the age is correct. Once the next DOB happens, the age stays the same. It does not increase by a year, as it should each time the DOB is reached on the calendar. But if I delete the DOB and re-enter the same DOB, the age updates to the correct age. This is what happened with the old code and the new one from Gilad D. What happens on your end when you enter the DOB as tomorrow? Does the age update itself the next day?

                                                              • 28. Re: Age field based on date field
                                                                GKaiseril CommunityMVP


                                                                If you force the PDF to recalculate by changing a field or resetting a field used in a calculation, then all the scripts in the calculation action will be recalculated.

                                                                 

                                                                Since you know so little, others need to see the form and the calculations.

                                                                • 29. Re: Age field based on date field
                                                                  WillCD Community Member

                                                                  //Since you know so little//

                                                                   

                                                                  Fair enough. Here you go.

                                                                   

                                                                  Connected Data, Inc.

                                                                   

                                                                  Check out field a102.

                                                                  • 30. Re: Age field based on date field
                                                                    WillCD Community Member

                                                                    So, did you guys see the form?

                                                                    • 31. Re: Age field based on date field
                                                                      Gilad D (try67) CommunityMVP

                                                                      I don't think anyone is going to install an unknown browser plugin in order to help you out. If you want help provide a direct link to the file.

                                                                      • 32. Re: Age field based on date field
                                                                        WillCD Community Member

                                                                        Unknown? I thought Transporter was well-known. How do I provide a direct link to the file?

                                                                        • 33. Re: Age field based on date field
                                                                          Gilad D (try67) CommunityMVP

                                                                          I've never heard of it... Upload the file to acrobat.com or dropbox or mega or any of the other free and easy-to-use file-sharing web-sites and post the sharing link to it here.

                                                                          • 34. Re: Age field based on date field
                                                                            WillCD Community Member

                                                                            Here you go. Thanks.

                                                                             

                                                                            Dropbox - delete.pdf

                                                                            • 35. Re: Age field based on date field
                                                                              Gilad D (try67) CommunityMVP

                                                                              Replace in the code you got the instances of this.getField("a102").value with event.value and it works just fine...

                                                                              • 36. Re: Age field based on date field
                                                                                WillCD Community Member

                                                                                Gilad D, are you understanding the problem? The problem is getting the field to update when a birthday happens. I changed the code like you said and it did not work. I entered a DOB of 10/29/2013 on 10/28/2014. The age correctly populated as 0. But today, it should read 1 and it still reads 0. Thoughts?

                                                                                • 37. Re: Age field based on date field
                                                                                  GKaiseril CommunityMVP

                                                                                  I would use hidden form field that contains today's date and update that field when the form opens and that would force the calculation of the age. I would also use that hidden form field in the calculation for the age.

                                                                                  • 38. Re: Age field based on date field
                                                                                    WillCD Community Member

                                                                                    Thanks. I would not know how to do that. What is the form using to calculate the date when the DOB is first entered? Wouldn't that value be dynamic? Shouldn't this work without having to make a hidden form field?

                                                                                    • 39. Re: Age field based on date field
                                                                                      Gilad D (try67) CommunityMVP

                                                                                      You can add  a special condition to the code that if the date and month are

                                                                                      the same it should add one more year to the age. No need for extra fields.

                                                                                      1 2 Previous Next