3 Replies Latest reply on Aug 21, 2012 1:29 PM by gkaiseril

    trouble with date fields

    Formzet - Zoetermeer

      i always have trouble calculating date fields.

       

      I have two fields "birthday" and a second field "reference date"

       

      the formatted value of the fields: ddmmyyyy

       

      I have to validate on the second field if that field isn't in the future and isn't older than the first field.

      Can onyone tell me how to perform such a validation?

        • 1. Re: trouble with date fields
          Formzet - Zoetermeer Level 1

          i've been able created a script that works, but sometimes i get strange things.

           

          As an example:

          when i enter a birthday of 07071975 (ddmmyy) and a reference date of 12072012 (ddmmyy) and later change the birthday tot 07072012 (ddmmyy), i get the message that te birthday date is newer than the reference date.

          But when i type it again, it accepts the change.

           

          Second example:

          Leaving birthday empty but entering the reference date 31122009 (ddmmyyyy)

          After that i entered the birthday date 01012010 (ddmmyyyy)

          The script strangely accepts it, but when i change the birthday to 02012010 (ddmmyy) i get the the appropriate error message.

           

          Is there someone who can look at this script?

           

          //Curent date
          var vNewDate = util.printd("ddmmyyyy", new Date());
          
          
          / / Conversion of current date for further calculation
          / / Time is removed and put to zero.
          var vNewDate2 = util.scand("ddmmyyyy",vNewDate.valueAsString);
          
          
          //Date 1
          var vNaam = "Datum overlijden";
          var vDatum1 = this.getField("datum_1.4").value;
          var d1 = util.scand("ddmmyyyy", this.getField("datum_1.3").valueAsString);
          
          
          //Date 2 (this field)
          var d2 = util.scand("ddmmyyyy", event.value);
          
          
          //Calculation of Date1 minus Date2
          var diff1 = Math.floor(d1.valueOf() - d2.valueOf());
          
          
          //Calculation current date minus Date 2
          var diff2 = Math.floor(vNewDate2.valueOf() - d2.valueOf());
          
          
          if (vDatum1 != "" && event.value != "" && diff1 < 0 && diff2 < 0)
          {       
              app.alert("Date can not be in the future, and newer than \""+vNaam+"\". Enter a valid date if you please");
              event.value = "";
              event.target.setFocus();
          }
          else if (vDatum1 != "" && event.value != "" && diff1 < 0 && diff2 >= 0)
          {       
              app.alert("Date can not be later than \""+vNaam+"\". Enter a valid date if you please");
              event.value = "";
              event.target.setFocus();
          }
          else if (vDatum1 != "" && event.value != "" && diff1 >= 0 && diff2 < 0)
          {       
              app.alert("Date can not be in the future. Enter a valid date if you please");
              event.value = "";
              event.target.setFocus();
          }
          
          
          else if (vDatum1 == "" && event.value != "" && diff2 < 0)
          {       
              app.alert("Date can not be in the future. Enter a valid date if you please");
              event.value = "";
              event.target.setFocus();
          }
          else
          {}
          
          
          • 2. Re: trouble with date fields
            gkaiseril MVP & Adobe Community Professional

            You need to convert the date string into a value where the date values are into a sequence where the dates are represented by an increasing numerical value.

             

            For example, one assigns "0" to Jan 1, 1970 (01Jan1970), "1" to Jan 2, 1970 (02Jan1970) and so on. One could then compare the numerical value associated with each date and by comparing those values one could tell if a one date was before or after another date.

             

            JavaScript provides such a feature in the Date object.and assigns a numeric value to the date and time value in milliseconds since January 1, 1970 12:00 am.

             

            With Acrobat one uses the 'util.scand' method to convert the string date value to a numeric value.

             

            For example if one entered the following code into the Acrobat JavaScript console and executed it:

             

            "01Jan1970" > "01Dec1970"

             

            the result would be "true" because with alphanumeric strings "01Jan1970" would occur after "01Dec1970".

             

            But if one converts the strings to sequential numbers like "0" for 01Jan1970 and "28879200465" for 01Dec1970, then comparison of the numeric values would be correct.

             

            Number(util.scand("ddmmmyyyy", "01Jan1970")) > Number(util.scand("ddmmmyyyy", "01Dec1970"));

             

            Will result in a  "true" result.

            • 3. Re: trouble with date fields
              gkaiseril MVP & Adobe Community Professional

              Format is very important. You might want to check to make sure that the conversion did not return a null value indicating an error between the format string and the date value.

               

              "Returns

              The converted Date object, or null if the conversion fails."

               

              You might want to include some separator characters for clarity in reading and scripting.

              .

              I would also check and only perform the calculations if both input fields have data. You may also need to check the order of your field calculations.