12 Replies Latest reply on Mar 28, 2015 12:37 PM by hasand59089870

    Acrobat Calculated Date JavaScript

    miked2931979

      Hi, I was wondering if anyone may assist me with this.

      I have a form that people complete for travel. Certain things have to be submitted not later than specific days before their travel.

       

      Beginning of form has travel date Q.1: mm/dd/yy

      Based on the travel date, there are a few deadlines to complete a few items.

      For example-

      Q2. submit form 0900 no later than [mm/dd/yy] --> here it spits out a date that is travel date info submitted above minus, say, 60 days.

       

      So on and so forth. I would be able to apply the rule to several questions, but would appreciate if someone may assist with the proper script to insert for this?

       

       

      Thanks a bunch!!

        • 1. Re: Acrobat Calculated Date JavaScript
          gkaiseril MVP & Adobe Community Professional

          What is the time zone for the cutoff time?

           

          Is it different from the local time zone?

           

          The logic for the calculation is to convert the date string to a date object from a n Epoch date. The Epoch date is Midnight January 1, 1970 UTC. This will convert any local date and time string to an object whose value will be the number of milliseconds since the Epoch date. This will create a unique value for each millisecond since the Epoch date without the affect of local time zone offsets. The object also contains information about the local time zone offset so the local time can be computed from the UTC time value.

           

          Now one only needs to use the appropriate calculation or method to adjust the date.

           

          A sample script:

           

          // Q1 date of "04/01/2015"
          var Q1 = "04/01/15";
          // Q1 convert to date object;
          var oQ1 = util.scand("mm/dd/yy", Q1);
          if(oQ1 == null) app.alert("Error converting " + Q1 + " using format mm/dd/yy", 1, 0);
          // display the date and time value for Q1;
          app.alert("Q1 is " + util.printd("mm/dd/yy HH:MM", oQ1), 3, 0);
          // subtract 60 days
          if(oDate != null) {
          // compute the Q2 date;
          var oQ2 = new Date(oQ1.getFullYear(), oQ1.getMonth(), oQ1.getDate() - 60, 9, 0, 0, 0);
          // display the computed Q2 date;
          app.alert("Q2 is " + util.printd("mm/dd/yy h:MMtt", oQ2));
          } else {
          app.alert("Q2 could not be computed.", 1, 0);
          }

          • 2. Re: Acrobat Calculated Date JavaScript
            miked2931979 Level 1

            Thank you for the info! I tried adjusting and implanting as a "Run a Javascript" in form and it said it had errors - which I couldn't resolve. I attempted trying again without any of my edits, and it still said there were errors?

             

            Any troubleshooting advice?

            • 3. Re: Acrobat Calculated Date JavaScript
              gkaiseril MVP & Adobe Community Professional

              "errors" is not very helpful because there are so many possible errors. Coding requires very specific and correct data and does not tolerate deviation form the required standard.

               

              What are the exact errors?

               

              What is the adjusted code?

               

              If I change the date string value or the number of days I do not get an error. Those are the only 2 variables. If you change any instance of a variable, you need to change all the instances of the variable to agree to the changed value.


              You may need to learn more about JavaScript and use the freely available Acrobat JavaScript documentation and the free MDN JavaScript documentation to learn about JavaScripting.

              • 4. Re: Acrobat Calculated Date JavaScript
                miked2931979 Level 1

                I wholeheartedly agree with you on that.. I'm still learning, so I'll look at it again.


                Thank you again for your help. I'll make it applicable to my form and then if it doesn't work I'll show my example.

                • 5. Re: Acrobat Calculated Date JavaScript
                  miked2931979 Level 1

                  calculation.JPG

                  Above ^ is the form with notes to capture my goal visually..

                   

                  So I went through the code and found the minor issue - I think... I converted the Q1-->TRAVEL_Date & Q2--> SUBMIT_NO_LATER (to reflect fields in my form).

                  I think the issue I run into with the code provided is the field Travel_Date (originally Q1), it is blank until the user fills out the Travel_Date field. Once they complete it, only then (based on the information the user enters in the first field), does the second field (Submit_No_Later) automatically has a date generated.

                  The items in bold are "unknown" until the user completes the info. Is there a way to make that information populated based off of what is entered in Q1/Travel_Date, that will in effect translate to the next field --> SUBMIT_NO_LATER?

                   

                  // TRAVEL_DATE date of "04/01/2015"
                  var TRAVEL_DATE = "04/01/15";
                  // TRAVEL_DATE convert to date object;
                  var oTRAVEL_DATE = util.scand("mm/dd/yy", TRAVEL_DATE);
                  if(oTRAVEL_DATE == null) app.alert("Error converting " + TRAVEL_DATE + " using format mm/dd/yy", 1, 0);
                  // display the date and time value for TRAVEL_DATE;
                  app.alert("TRAVEL_DATE is " + util.printd("mm/dd/yy HH:MM", oTRAVEL_DATE), 3, 0);
                  // subtract 60 days
                  if(oDate != null) {
                  // compute the SUBMIT_NO_LATER date;
                  var oSUBMIT_NO_LATER = new Date(oTRAVEL_DATE.getFullYear(), oTRAVEL_DATE.getMonth(), oTRAVEL_DATE.getDate() - 60, 9, 0, 0, 0);
                  // display the computed SUBMIT_NO_LATER date;
                  app.alert("SUBMIT_NO_LATER is " + util.printd("mm/dd/yy h:MMtt", oSUBMIT_NO_LATER));
                  } else {
                  app.alert("SUBMIT_NO_LATER could not be computed.", 1, 0);
                  }

                  • 6. Re: Acrobat Calculated Date JavaScript
                    hasand59089870 Level 2

                    function adddays(xDate, xAddDays){
                      var date1 = new Date(xDate)
                      var ndate1 = Date.parse(date1);
                      var ndate2 = ndate1 + 86400000 * xAddDays
                      var date2 = new Date(ndate2)
                      return util.printd("mm/dd/yy", date2)
                    }

                     

                    var Q1date = this.getField("Q1").value
                    var daystoadd = 60
                    this.getField("Q2").value = adddays(Q1date, daystoadd)

                    • 7. Re: Acrobat Calculated Date JavaScript
                      miked2931979 Level 1

                      Okay, got help and had my question answered.

                       

                      If anyone else would need it, this is the code that worked for me.

                       

                      var td = this.getField("Travel_Date").value;


                      var d1 = util.scand("mm/dd/yy", td);
                      var d0 = d1.valueOf() - 40 * 24 * 60 * 60 * 1000;

                      var snl = new Date(d0);

                      event.value = util.printd("mm/dd/yy", snl);

                      • 8. Re: Acrobat Calculated Date JavaScript
                        gkaiseril MVP & Adobe Community Professional

                        Your original question was for 60 days and not 40 days. Also if you come back in a year and need to update the code will you remember what the formula is and what the variables stand for?

                         

                        Documentation and commenting about how code works is also part or programming and good practices.

                        • 9. Re: Acrobat Calculated Date JavaScript
                          miked2931979 Level 1

                          My apologies for any confusion. In my original question, I was us 60 days as an example. My form contains several fields which have different deadlines, so there are some with 20, 40, 60 and 80.

                          I have noted a lot of the items in the script for quick manipulation in the future. Thank you again for your help.

                          • 10. Re: Acrobat Calculated Date JavaScript
                            hasand59089870 Level 2

                            do you tested my script. Its similiar. And about 1,5 hours earlier. I find you stole my idea and mark yourself as the right answer. At least I would have expected a comment to my contribution I find it's and you are not correct.

                            In future i want loose my time to try help you

                            • 11. Re: Acrobat Calculated Date JavaScript
                              miked2931979 Level 1

                              My apologies. I had not checked in-between the times. But I adjusted and gave "correct" credit to you.

                              • 12. Re: Acrobat Calculated Date JavaScript
                                hasand59089870 Level 2

                                Sorry I have misjudged you badly

                                Now the world is right again.

                                 

                                Thanks..

                                Not because of the points.

                                Because you have taken position and because you created clarity