6 Replies Latest reply on Feb 8, 2017 6:22 AM by yuioh

    Calculating an end date

    vanreed

      I've searched and searched and can't seem to find a script to help me out.  I need to calculate an end date for a time period.  If i enter the begining date, I need the calculation to determine the end date based on 14 days after the begining date.  Is this possible?  Using Acrobate 9 Pro.

        • 1. Re: Calculating an end date
          try67 MVP & Adobe Community Professional

          The best way is to add the amount of time you want in milliseconds.

          So if you have a Date object called time, you can do something like this to

          add 14 days to it:

           

          time.setTime(time.getTime()+(100060602414))

          • 2. Re: Calculating an end date
            vanreed Level 1

            Will that populate my end field with a date?  I'm going to try in now.

            • 3. Re: Calculating an end date
              George_Johnson MVP & Adobe Community Professional

              The general script would look something like:

               

               

              // Get date from field

              var v = getField("your_date_field").value;

               

              // Convert string to date

              var d = util.scand('mm/dd/yyyy', v);

               

              // Add 90 days
              d.setDate(d.getDate() + 14);

              • 4. Re: Calculating an end date
                gkaiseril MVP & Adobe Community Professional

                You may need to modify the format picture of the date to match the format picture of you starting date field.

                 

                If you have only numbers in that field, then you will need to force the value to a string.

                 

                You are converting the string value of your start date to the JavaScript date object and then either adding the necessary milliseconds for the getTime variation or the number of day for the getDate variation.

                 

                All of the above assume you are using Acrobat as the form editor and not LiveCycle Designer. If you are in LiveCycle Designer look at the included Scripting Reference for the Date2Num and Num2Date functions.

                • 5. Re: Calculating an end date
                  George_Johnson MVP & Adobe Community Professional

                  Here's what a custom calculate script would look like for the "to" field:

                   

                  // Custom calculate script for "to" field
                  (function () {
                  
                      // Get date from field
                      var v = getField("from_date").valueAsString;
                  
                      // Convert string to date
                      var d = util.scand("mm/dd/yyyy", v);
                  
                      // Add 14 days
                      d.setDate(d.getDate() + 14);
                  
                      // Populate this field with the result
                      if (v) {
                          event.value = util.printd("mm/dd/yyyy", d);
                      } else {
                          event.value = "";
                      }
                  
                  })();
                  

                   

                  Change "from_date" to the name of your starting date field and the format string "mm/dd/yyyy" to what you're using.

                  2 people found this helpful
                  • 6. Re: Calculating an end date
                    yuioh

                    This works like a charm. Thank you!!