1 Reply Latest reply on Jan 24, 2014 1:43 PM by gkaiseril

    I am trying to make a timecard with adobe acrobat pro x.

    Tom_Foolery43

      I want to fill the dates for each day on the time card working backwards from the date in the field "EndDate" which is for pay period end.  My simple js line is:

       

       

      event.value = new Date(new Date().setDate(new Date(getField("EndDate").value).getDate()-1))

       

       

      When i enter this, the console window shows this error:

       

       

      InvalidSetError: Set not possible, invalid or unknown.

      Event.value:1:Field DateFri2:Calculate

       

       

      When i run this code in the console window it works and returns a date 1 less than what is present in "EndDate".   So for example i enter "01/01/2014" in for EndDate without the quotes of course.  Then run the above line in the console window.

      This is what i get as output:

       

      Tue Dec 31 2013 13:49:01 GMT-0600 (Central Standard Time)

       

      Proving that the line works.  So why does it fail when i place in the "Custom calculation script:" menu.

       

      Am i missing something in my code or am i using a method that is not supported in adobe acrobat pro x?  Any assistance would be greatly appreciated.  Thank you.

        • 1. Re: I am trying to make a timecard with adobe acrobat pro x.
          gkaiseril MVP & Adobe Community Professional

          It might be just luck that the code works in the console.

           

          Have you looked at the various references about working with dates?

           

          Have you accounted for the situation were fields are empty?

           

          What happens when one enters "24/1/2014" or '1/24/2014"?

           

          Are they the same dates?

           

          Compressing code to the smallest possible line can introduce many errors. It is better to write some additional lines of code so one can easily add debugging statements or stop points to allow for testing of values and results..

           

          I prefer to use the util.scand method to convert a date string to the date object. It also provides the chance to validate the date string being used.

           

          I would also look at using functions since there will multiple calculations that are identical except for the input values. With document level functions for repeatedly used code means if an error is found in the calculation you only need to correct it at one place.

           

          You maybe using the util.scand metnod for converting time strings to date objects so you can get the minutes and hours needed for the time sheet calculations.

           

          JavaScript is very sensitive to spelling and capitalization of objects, properties, methods, variables, function names, etc.

           

          // document level scripts;
          function GetField(cName) {
          // return the field object for named field;
          var oField = this.getField(cName);
          if(oField ==  null) app.alert("Error accessing field named " + cName, 0, 0);
          return oField;
          } // end GetField function;

          function Scand(cFormat, cValue) {
          // convert string value to date object using provided format;
          var oDate = util.scand(cFormat, cValue);
          if(oDate == null) app.alert("Error converting date/time " + cValue + " with format " + cFormat, 0, 0);
          return oDate;
          } // end Scand function;

          function AddDays(oDate, nDays, nHrs, nMins, nSecs, nMillisecs) {
          if(typeof nHrs == "undefinded") var nHrs = 0;
          if(typeof nMins == "undefinded") var nMins = 0;
          if(typeof nSecs == "undefinded") var nSecs = 0;
          if(typeof nMillisecs == "undefinded") var nMillisecs = 0;

          // add number of days to date;
          return new Date(oDate.setDate(oDate.getDate() + nDays, oDate.getHours() + nHrs, oDate.getMinutes() + nMins, oDate.getSeconds() + nSecs, oDate.getMilliseconds() + nMillisecs ));
          } // end AddDays funciton;
          // end document level scripts;

           

          // custom calculation for day before end date field;

          // get start date field value;
          var oEndDate = GetField("EndDate");
          // get date object for the end date;
          var oEndDate = Scand("dd-mm-yyyy", oEndDate.valueAsString);
          // adjust date
          oEndDate = AddDays(oStartDate, -1);
          event.value = util.printd("dd-mm-yyyy", oEndDate);

          // end custom calculation script;

           

          There is no "Event" object so one can not set any property to a non-existent object. The object is "event".