3 Replies Latest reply on Jul 9, 2015 9:15 AM by gkaiseril

    Enter one date and autocalculate dates for the week

    Alyksandrei

      I'm trying to figure out how to write the code that would let me autocalculate a series of dates based on a single input. It's been a while since I did any coding, and I was never all that good, so any pointers or suggestions with sample code are best.

       

      I have a (rather large) form, and I need to have dates for each day of the week, one date per page (each date appears multiple times; I've got that covered already). I keep making mistakes when filling the dates in by hand, so I'd like to input Monday's date and have it fill in the dates for the rest of the week. Ideally, the visible output for each date would be in the format "Thursday, 9 July 2015", although any format that includes day-of-week as well as the month-day-year would be acceptable.

       

      I'm really kinda stuck on this, and basically have no idea how to even start...

        • 1. Re: Enter one date and autocalculate dates for the week
          gkaiseril MVP & Adobe Community Professional

          Dates and times are not simple.

           

          Did you adjust your computer for recent leap second that occurred last month?

           

          With the shrinking of the world or faster communications, one may need to allow for time zone differences. Fortunately your computer and Acrobat/Reader do this automatically by using the UTC time and data and using the time zone offset on your computer to adjust the displayed date and time.

           

          Acrobat JavaScript use the JavaScript date object that is the number of milliseconds since midnight on January 1, 1970 and the Royal Observatory at Greenwich, England.

           

          You will need to convert your date string to the Date object and then add the number of days as needed either by computing the milliseconds in a day or using the date object and using the setDate and getDate methods.

          For formatting the dates and day of the week one can use the util.printd method.

           

          The following script will display the next 7 days:

           

          // get the starting date value;

          // var cStart = this.getField("StartDate").value;

          var cStart = "6-Jul-2015";

          // format stirng for input date;

          var cStartFormat = "d-mmm-yyyy";

          var cDateDay; // variable for the day and day of week;

          var cDateDayFormat = cStartFormat + " dddd";;

          // process non-blank input values

          if(cStart != "") {

          // convert date value to date object;

          var oStart = util.scand(cStartFormat, cStart);

          var oDate = oStart;

          if(oStart ==  null) app.alert("Unable to convert " + cStart + " to date object using " + cStartFormat, 1, 0);

          if(oStart != null) {

          for(var i = 0; i < 6; i++) {

          oStart = util.scand(cStartFormat, cStart); // reset to start date object;

          // update the date object by the date of the week;

          oDate.setDate(oStart.getDate() + i);

          // display/set the date field;

          cDateDay = util.printd(cDateDayFormat, oDate);

          console.println( (I + 1) + " " + cDateDay);

          // code to set the appropriate date field;

          } // end day loop;

          } // end oStart not null;

          } // end input date not null;

          • 2. Re: Enter one date and autocalculate dates for the week
            Alyksandrei Level 1

            Still really confused - how do I plug that script into my form so that I can see if it works for me?

            • 3. Re: Enter one date and autocalculate dates for the week
              gkaiseril MVP & Adobe Community Professional

              I would look at using this as an on blur action for the field into which enter the starting date.

               

              There are a number of changes that need to be made to fit your particular form like the format for the dates and days and you need code to populate the sequence of day fields using their names. With careful planning one can create a series of field names than be computed using JavaScript.