1 Reply Latest reply on Jun 25, 2015 1:00 PM by gkaiseril

    Java Script for DOB on specific date

    210allstar

      We have a registration form that we use online for our youth football team. I need a script that will automatically calculate the age as of 08/01 based on the DOB field. I have searched many scripts but have yet to find one that works based on a single field and a date that is not "today".

        • 1. Re: Java Script for DOB on specific date
          gkaiseril MVP & Adobe Community Professional

          You could change the date object that represents "today" to the date object created for your specified date.

           

          I have posted at least one for Little League.

           

           

          Here is a script that will do it for using field for the cutoff, a string variable, or the system date depending upon the values for certain cut off  date options;

           

          // document level scripts;
          function GetField(cName) {
          var oField = this.getField(cName);
          if(oField == null) app.alert("Error opening field \"" + cName + "\"", 1, 0);
          return oField;
          } // end GetField function;

          function Scand(cFormat, cDate) {
          var oDate = util.scand(cFormat, cDate);
          if(oDate == null) app.alert("Error converting date \"" + cDate + "\" with a format of \"" + cFormat + "\"", 1, 0);
          return oDate;
          } // end Scand function;
          // end document level scripts;

          var cDateFormat = "mm/dd/yyyy"; // format for date fields;
          var cDobField = "DOB" // dob field name;
          var cCutOffDateField = ""; // cutoff date field; // null does not use this feature;
          var cCutOffDateString = "08/01/2015"; // cutoff date string in script; // null does not use this feature;
          var nAge ="";
          // get the date of birth field value
          var cDobDate = GetField(cDobField).valueAsString;

          // set up cut-off date value form field, string or system date base on values for field name and string;
          if(cCutOffDateField != "") cCutOffDate = GetField("cCutoffDateField").valueAsString; // for date from a field;
          else if(cCutOffDateString != "") cCutOffDate = "08/01/2015"; // for specified date string;
          else cCutOffDate = util.printd(cDateFormat, new Date()); // for current system date;
          // process if dob and cutoff dates are not empty;

          if(cDobDate != "" && cCutOffDate != "") {
          // convert dob value to date object;
          var oDob = Scand(cDateFormat, cDobDate);
          // convert cutoff value to date object;
          var oCutOff = Scand(cDateFormat, cCutOffDate);
          // compute difference in years;
          var nAge = oCutOff.getFullYear() - oDob.getFullYear();
          // adjust 1 year less for cutoff month before month for Dob;
          if(oCutOff.getMonth() < oDob.getMonth()) nAge--;
          // adjust 1 year less for cutoff month same as dob month and cutoff date before dog date;
          if(oCutOff.getMonth() == oDob.getMonth() && oCutOff.getDate() < oDob.getDate()) nAge--;
          }
          event.value = nAge;