12 Replies Latest reply on Sep 29, 2015 8:34 AM by sb64284027

    Age based on Date of Birth Field

    IamNathan

      The problem that I am running into is that I have a field "date of birth" which a user fills out. There are only 8 spaces allowed meaning mmddyyyy with no "/" seperating them.

       

      Here is the code I have been working with:

       

      event.value = "";

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

      }

       

      I have tried to remove the "/" from "dd/mm/yyyy" I have also tried to flip it to show mmddyyyy but still no luck. If I do enter 1/1/1990 it will return the correct age of "23"

       

      Any ideas, while running the javascript debugger it is stating: dob is null

       

      Thanks as always,

       

      Nathan

        • 1. Re: Age based on Date of Birth Field
          try67 MVP & Adobe Community Professional

          It should work just fine, as long as you're entering a DOB value using the

          correct pattern.

          • 2. Re: Age based on Date of Birth Field
            IamNathan Level 1

            That's the issue, the only pattern I can enter is mmddyyyy "12102013"

            • 3. Re: Age based on Date of Birth Field
              try67 MVP & Adobe Community Professional

              That should work... If it doesn't, can you share the file?

              • 4. Re: Age based on Date of Birth Field
                IamNathan Level 1

                What is the best way to share the file? Dropbox or does Adobe have something set up?

                • 5. Re: Age based on Date of Birth Field
                  try67 MVP & Adobe Community Professional

                  Dropbox is fine. Or you can use Acrobat.com, or something similar... Post

                  the link to it here.

                  • 6. Re: Age based on Date of Birth Field
                    IamNathan Level 1

                    https://www.dropbox.com/sh/uyeowlmqvtbuvqj/ndUrBqAqCC

                     

                    I have only set up the first date of birth field

                    • 7. Re: Age based on Date of Birth Field
                      try67 MVP & Adobe Community Professional

                      You didn't update the script of the "age" field...

                      • 8. Re: Age based on Date of Birth Field
                        gkaiseril MVP & Adobe Community Professional

                        Are you getting any errors in the JavaScript console.?

                         

                        Since you are creating a possible number from the dob value, you should use the valueAsString property to keep the value as a string and not let JavaScript convert it to a floating point number.

                         

                        Why are you using a different format for the date of birth string conversion than the format of the field?

                         

                        event.value = "";
                        var cDobField = "date of Birth"; // name for dob field;
                        var cDobFormat = "mmddyyyy"; // format for date of birth;
                        var oField = this.getField(cDobField, 0, 0);
                        if(oField == null) app.alert("Error accessing field " + cDobField);
                        var dobValue = getField("date of Birth").valueAsString;
                        if (dobValue!="") {
                        var dob = util.scand(cDobFormat, dobValue);
                        if(dob == null) app.alert("Error converting date string " + dobValue  + " with format " + cDobFormat, 0, 0);
                        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);
                        }


                        • 9. Re: Age based on Date of Birth Field
                          IamNathan Level 1

                          I was getting errors, null errors. As for the different format I had tried changing it multiple times, but it appears that main cause of the error was that I wasn't using the value as a string.

                           

                          Thanks for all your help!

                          • 10. Re: Age based on Date of Birth Field
                            RJKnyumc

                            event.value = "";

                            dobValue = getField("DOB").value;

                            if (dobValue!="") {

                            dob = util.scand("mm/dd/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

                            }

                             

                            This is what I always use, it gets the right age once the date exceeds DOB

                            • 11. Re: Age based on Date of Birth Field
                              gkaiseril MVP & Adobe Community Professional

                              But will it work with the provided requirements? The format string for the date does not match the format as needed by the OP. Also there is an issue with JavaScript taking a string of numbers and assuming that string is a floating point number and not a character string. The util.scand method requires a character string and not a floating point number.

                               

                              The following code issues alerts for wrong field name or error in converting the date string to a date object.

                               

                              event.value = "";

                              var cDobField = "date of Birth"; // name for dob field;

                              var cDobFormat = "mmddyyyy"; // format for date of birth;

                              var oField = this.getField(cDobField);

                              if(oField == null) app.alert("Error accessing field " + cDobField);

                              var dobValue = getField(cDpbField).valueAsString;

                              if (dobValue!="") {

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

                              if(dob == null) app.alert("Error converting date string " + dobValue  + " with format " + cDobFormat, 0, 0);

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

                              }

                              • 12. Re: Age based on Date of Birth Field
                                sb64284027

                                I just tried this script and played with the DOB dates and found it didn't provide the correct age for either exactly 1year or 1 year and 1 day prior.

                                That is if current date is: 290915

                                DOB is either 290914 or 280914 the age came as being 0

                                 

                                This also continues for previous years.

                                 

                                Is this an error with the Math.floor or me?