2 Replies Latest reply on Jul 19, 2016 9:52 AM by gkaiseril

    Date Calculations

    scudtater

      im creating a timesheet and im trying to make the date automatically configure after the first date is entered

        • 1. Re: Date Calculations
          gkaiseril MVP & Adobe Community Professional

          Do you need validation of the starting day of the week or the ending day of the week?

           

          Do you know how to do any date arithmatic in JavaSvript?

           

          Study the util.scand method and JavaSvript's date methods.

           

          Careful planning of field named can make tje scripting task eadier. I would use higherical field mames.

           

          This question has been asked many times, please sesrch for the posts.

          • 2. Re: Date Calculations
            gkaiseril MVP & Adobe Community Professional

            Using a hierarchal field with a parent name of "WeekDay" and children #", one can use a validation script like in the field for the "period ending" field:

             

            if(event.value != "")
            {
            var oDate = Scand("d-mmm-yyyy", event.value);
            if(oDate != null)
            {
              // test for valid ending day of week;
              if(oDate.getDay() != 6)
              {
               var cMsg = "Week must end on a Saturday!";
               cMsg += "\n" + util.printd("d-mmm-yyyy", oDate) + " falls on a " + util.printd("dddd", oDate) + "!";
               cMsg += "\nPlease renter a date that falls on a Saturday.";
               app.alert(cMsg, 1, 0);
               // reset form the field;
               this.resetForm([event.target.name, ["WeekDay"]]);
               // set system return code for script;
               event.rc = false;
              }
              else
              {
               var oDays = GetField(this, "WeekDay"); // get parent field object;
               var aDays = oDays.getArray(); // create array of children field objects from the parent object;
               oDate.setDate(oDate.getDate() - (aDays.length - 1));
               var nDate = oDate.getTime();
               var nOneDay = 1000 * 60 * 60 * 24;
               for(var i = 0; i < aDays.length; i++)
               {
                // GetField(this, "WeekDay" + "." + i).value = newW Date( nDate + (i * nOneDay)); // compute field name;
                aDays[i].value = new Date( nDate + (i * nOneDay)); // get i field object from array of children field objects;
               } // end date loop;

              } // end if weekending day of week;
            } //
            }
            else
            {
            this.resetForm(["WeekDay"]);
            } // end field value not null;

             

             

            A format script for the "WeekDay.#" field is:

             

            if(event.value != "")
            {
            event.value = WeekDayFormat(event.value;
            }

             

            The following document level scripts are needed to support the above code:

             

            function Scand(cFormat, cDate)
            {
            /*
            purpose: convert date string with format to JS Date object with error reporting for invalid date strings;
            inputs:
            cFormat - image representation of date and time format;
            cDate - date string to convert;
            returns:
            JS date if a valid date else null;
            */
            // convert date time string to date object;
            var oDate = util.scand(cFormat, cDate);
            // test for error;
            if(oDate == null)
            {
              // report error;
              app.alert("Error occured converting date " + cDate + " with format of " + cFormat + "!", 1, 0);
            }
            // return date object or null if error;
            return oDate;
            } // end Scand funciton;

            function GetField(oDoc, cName)
            {
            /*
            purpose: return field object for field name in document object;
            inputs:
            oDoc -  document object to use;
            cNmae - name of field to process;
            retun: field object or null if not found or other error;
            */
            // get field object;
            var oField = oDoc.getField(cName);
            // test for error;
            if(oField == null)
            {
              // error occurred;
              app.alert("Error accessing field " + cName + "!\nPlease check that document exist and field name is correct.", 1, 0);
            }
            // return field object or null if error occurred;
            return oField;
            } // end GetField function;

            function WeekDayFormat(cValue)
            {
            if(cValue != "")
            {
              return util.printd("d-mmm-yyyy dddd", new Date(cValue));
            }
            }

             

            Note the above code determines the number of daily fields to populate from the number of children under the parent "WeekDay" field.