19 Replies Latest reply on Apr 8, 2015 6:44 PM by bpwhistler

    Script to determine days between dates

    bpwhistler Level 1

      I'm totally new to JavaScript.  I'm attempting to do a calculation field with a custom calculation script.  I've tried to piece together something with script I've scavenged from multiple forums, but have been completely unsuccessful.  I have 4 form fields.  2 date fields, a number field (formatted for currency), and the calculation field.  The calculation I'm attempting is ((365 - (days between the 2 date fields)) divided by 365) times (currency field).  Any help would be greatly appreciated.

        • 1. Re: Script to determine days between dates
          hasand59089870 Level 2

          // the function adddays returns the calculated Date (as + or - days).

          // Q1date = new Date()

          // var daystoadd = -2          // two days before

          // var newDate = adddays(Q1date, daystoadd))

           

          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 date2   

            //return util.printd("mm/dd/yy", date2)        // if you want formated. Then adjust the printd and take this as return value
          }

           

          ------------------------------------------------------------------------------------------ ---------------------


          function daysBetween (startDate, endDate) {
              return Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(86400000)));
          };

           

          // Q1date = new Date()

          // Q2date = new Date("2015/04/08")

          // var xdaydif = daysBetween(Q1date, Q2Date)

          • 2. Re: Script to determine days between dates
            bpwhistler Level 1

            Thank you very much...but as I mentioned, I am VERY new to JavaScript.  In reality I don't know anything about it.  I ran into the need for some code on a work project.  If I can make this one simple form do some 'magical' things, I might be able to wrangle some education out of my boss.  I was hoping somebody might have a similar  script they'd be willing to share.  My problems are two fold.  I don't know JavaScript and don't know how to integrate it with Adobe Acrobat forms.  My date fields are date1 and date2.  My currency field is premium1.  And my calculation field is ProRate1.

             

            So my ultimate question is...how do I use those terms you supplied with my field names to get the correct answer in my calculation field?

            • 3. Re: Script to determine days between dates
              hasand59089870 Level 2

              Textfield "ProRate1" -> TextFieldProperties -> Calculate (Tab) -> Custom Calculation script

              copy the following code

               

              var startDate = this.getField("date1").value

              var endDate = this.getField("date2").value

              var xdays = Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(86400000)))

              event.value =  ((365 - (xdays)) / 365) * (this.getField("premium1").value)

              • 4. Re: Script to determine days between dates
                bpwhistler Level 1

                Thank you...I actually understand the logic behind that coding.  The syntax is beyond my ability to develop currently...but I'm hoping if I (you ) can make this work I will start learning JavaScript on the company dime.

                 

                I copied and pasted the above code into my 'custom calculation script' tab for my calculation field.  My date fields are formatted mm/dd/yyyy (company standard).  The premium1 field is formatted as a number with a currency symbol.  The calculation field (ProRate1) is also formatted as a number with a currency symbol.

                 

                With your pasted script and the above formatting of the fields I don't get any output into the calculation field when I enter data.

                 

                Do I need to use a different date format?  That is the format that has to show on the form...as I mentioned...company standard.

                • 5. Re: Script to determine days between dates
                  hasand59089870 Level 2

                  Textfield "ProRate1" -> TextFieldProperties -> Calculate (Tab) -> Custom Calculation script

                  copy the following code

                   

                  var myFormat = "mm/dd/yyyy"

                  var startDt = this.getField("date1").value
                  var endDt = this.getField("date2").value

                  var startDate = util.scand(myFormat,startDt);
                  var endDate = util.scand(myFormat,endDt);

                  var xdays = Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(86400000)))

                  event.value =  ((365 - (xdays)) / 365) * (this.getField("premium1").value)

                  • 6. Re: Script to determine days between dates
                    bpwhistler Level 1

                    OMG...you're a freaking genius.  My boss is going to love me.

                     

                    I have one last favor...the value produced by (365-(xdays))/365) needs to be rounded to 3 decimal places before multiplying by the premium1 value.  Is this possible?

                     

                    Is there a way I can pay you for this?  A PayPal donation for your time and patience?

                     

                    Brett

                    • 7. Re: Script to determine days between dates
                      gkaiseril MVP & Adobe Community Professional

                      I would try this custom calculation script to compute the premium refund:

                       

                      var myFormat = "mm/dd/yyyy"
                      var startDt = this.getField("date1").value
                      var endDt = this.getField("date2").value

                      function Scand(cFormat, cDate) {
                      // convert date string to date object with error checking;
                      var oDate = util.scand(cFormat, cDate);
                      if(oDate == null) app.alert("Error converting date " + cDate + " with format " + cFormat, 1, 0);
                      return oDate;
                      }
                      function Date2Num(oDate) {
                      // convert date object to number of days since Epoch date;
                      return Math.floor(oDate.getTime() / (1000 * 60 * 60 * 24));
                      }
                      function Round(nValue, nDec) {
                      // round value to number of decimal places;
                      return Number(util.printf("%,101.3f", nValue));
                      }
                      var startDate = Scand(myFormat,startDt);
                      var endDate = Scand(myFormat,endDt);
                      event.value = "";
                      var nEnforceDays = Date2Num(Scand(myFormat,endDt)) - Date2Num(Scand(myFormat,startDt));
                      if(xdays < 0) {
                      app.alert("End date cannot be before start date", 1, 0);
                      } else {
                        var nProrateDays = 365 - nEnforceDays;
                        var nProrateFactor = nProrateDays / 365;
                        event.value =  Round(nProrateFactor, 3) * this.getField("premium1").value;
                        }

                      • 8. Re: Script to determine days between dates
                        bpwhistler Level 1

                        Thanks...that's way beyond my understanding.  It didn't produce anything in the calculation field though.  Is there anything I can do to troubleshoot it?  I feel guilty about all the work you're putting into this.

                        • 9. Re: Script to determine days between dates
                          gkaiseril MVP & Adobe Community Professional

                          Open the JavaScript console and see if you have any errors.

                          • 10. Re: Script to determine days between dates
                            bpwhistler Level 1

                            I don't have access to the developer tools...

                            • 11. Re: Script to determine days between dates
                              gkaiseril MVP & Adobe Community Professional

                              Then how are you entering the code?

                              • 12. Re: Script to determine days between dates
                                bpwhistler Level 1

                                The script from post 5 actually worked perfectly...it was just a little to precise...

                                 

                                var myFormat = "mm/dd/yyyy"

                                var startDt = this.getField("date1").value
                                var endDt = this.getField("date2").value

                                var startDate = util.scand(myFormat,startDt);
                                var endDate = util.scand(myFormat,endDt);

                                var xdays = Math.round(Math.abs((startDate.getTime() - endDate.getTime())/(86400000)))

                                 

                                The value calculated up to this point is perfect.  It just needs to be rounded to an absolute 3 decimal places,

                                and that rounded value utilized in the final calculation.  I took the value calculated above and

                                formatted the field to a number field with 3 decimal places.  The problem is when I use it for additional calculations,

                                the calculation utilizes all the decimal places and not just the formatted/visible ones.

                                 

                                  I notice a step in the script from post 7 that mentions rounding to a number of decimal places...

                                 

                                function Round(nValue, nDec) {

                                // round value to number of decimal places;

                                return Number(util.printf("%,101.3f", nValue));

                                }

                                 

                                Would using the above code enable an absolute 3 decimal place result?

                                I could take an absolute 3 decimal number and use simple calculations in

                                another field to obtain the following rather than including it all in a single

                                field calculation.

                                event.value =  ((365 - (xdays)) / 365) * (this.getField("premium1").value)

                                 

                                Obviously not as clean...but if it works?

                                • 13. Re: Script to determine days between dates
                                  bpwhistler Level 1

                                  I'm entering the code in a form field under the calculate tab (custom calculation script section) in Adobe Acrobat Pro.

                                  • 14. Re: Script to determine days between dates
                                    bpwhistler Level 1

                                    I'm sorry...I misstated where the rounding needs to occur.

                                     

                                    It's at the 'event.value =  ((365 - (xdays)) / 365)' section.

                                     

                                    The result of this needs to be 3 decimal places for subsequent calculation.

                                    • 15. Re: Script to determine days between dates
                                      hasand59089870 Level 2

                                      event.value =  ( ((365 - (xdays)) / 365) * (this.getField("premium1").value) ).toFixed(3)

                                       

                                      Thank you for offer but donate isn't necessary. We can stay friends. Send me a mail at dorul-edv@web.de if you want..

                                      • 16. Re: Script to determine days between dates
                                        hasand59089870 Level 2

                                        Please, don't forget to mark the right answer. Thank you.

                                        • 17. Re: Script to determine days between dates
                                          bpwhistler Level 1

                                          I actually moved the .toFixed(3) here...event.value =  ( ((365 - (xdays)) / 365).toFixed(3) * (this.getField("premium1").value) )

                                           

                                          It works beautifully.  Thank you.  I will drop you an email when I'm off my work computer.

                                          • 18. Re: Script to determine days between dates
                                            gkaiseril MVP & Adobe Community Professional

                                            Here is link to a working copy of an example form:

                                             

                                            Insurance Premium Proration

                                            • 19. Re: Script to determine days between dates
                                              bpwhistler Level 1

                                              Thank you...I will work with that when I get to work tomorrow