16 Replies Latest reply on Apr 21, 2009 2:16 PM by RonMan62

    Javascript help

      I have a few questions.

      1) I need to add 14 days to today's date.

      2)I need to get the difference in days between two dates

      3)One of my fields is a read-only that involves dividing two fields together. However, I keep receiving error messages because when the form is blank the field keeps tring to divide by 0. How do I stop this??

      I know how to do all of this stuff is LiveCycle but I'm now using Acrobat 9 forms.

      Help with any of these questions would be greatly appreciated! Thank you.
        • 1. Re: Javascript help
          gkaiseril MVP & Adobe Community Professional
          1. You need to convert the date to the JavaScirpt date time object, convert the JS object to a value, add 14 days expressed as milliseconds to the value and then format the result as you want it displayed. More information is contained in Acrobat's JS APR Reference and Guide.

          /* Example of date arithmetic. */
          /* Create a Date object with a definite date. */
          var d1 = util.scand("mm/dd/yy", "4/11/76");
          /* Create a date object containing the current date. */
          var d2 = new Date();
          /* Number of seconds difference. */
          var diff = (d2.valueOf() - d1.valueOf()) / 1000;
          /* Print some interesting stuff to the console. */
          console.println("It has been "
          + diff + " seconds since 4/11/1976");
          console.println("It has been "
          + diff / 60 + " minutes since 4/11/1976");
          console.println("It has been "
          + (diff / 60) / 60 + " hours since 4/11/1976");
          console.println("It has been "
          + ((diff / 60) / 60) / 24 + " days since 4/11/1976");
          console.println("It has been "
          + (((diff / 60) / 60) / 24) / 365 + " years since 4/11/1976");

          2. See 1 above.

          3. You have to use an "if" statement to see if the divisor is zero and either not perform the division or take some other action.

          event.value = ''; // assume no division and clear result
          // if divisor is not zero
          if(this.getField("MyDivisor").value != 0)
          event.value = this.getField("MyDividend").value / this.getField("Mydivisor").value;
          • 2. Re: Javascript help
            Level 1
            Number 3 worked. Thank you.

            I don't understand your instructions for numbers 1/2. I don't see any options to convert the date or JS object.
            • 3. Re: Javascript help
              gkaiseril MVP & Adobe Community Professional
              The date script is an example of date time computation. You will have to add the code for using the value of a field rather than the date string of "4/17/76" and modify the date format string to describe the format of your date value.
              • 4. Re: Javascript help
                Level 1
                I'm sorry I'm still really confused about this. If I'm trying to add 14 days to today's date which script do I use? It looks as though the one you gave me is calculating the difference between two days (which I also have to do and this script doesn't work).

                Thanks for your help.
                • 5. Re: Javascript help
                  Level 1
                  Does anyone know how to do this stuff? I'm up against a deadline and I'm totally stuck with these last two questions! Thanks!
                  • 6. Re: Javascript help
                    Patrick Leckey Level 3
                    Add 14 days to today's date:

                    var date1 = new Date();
                    date1.setDate(date1.getDate()+14);
                    console.println(date1);

                    ----

                    Calculating days between 2 dates:

                    var date1 = new Date();
                    var date2 = new Date(date1.getFullYear(), 11, 25);
                    var oneDay = 1000 * 60 * 60 * 24;
                    console.println(Math.floor((date2.getTime() - date1.getTime())/oneDay));

                    "date1" and "date2" above are just examples, this will calculate the number of full days between now and Christmas. Substitue "date1" and "date2" with the 2 date objects you are using. This assumes date2 > date1.

                    How you would work this into your form is up to you since I have no way of knowing the layout or object names in your form.
                    • 7. Re: Javascript help
                      Level 1
                      Thank you but the calculating days between two dates script does not work. The fields I am using are Text43 (today's date) and Text51 (a date in the future that will be chosen by the user). I tried substituting these in for "date1" and "date2" but nothing happened. Did I miss something? Thanks for your help!
                      • 8. Re: Javascript help
                        gkaiseril MVP & Adobe Community Professional
                        I prefer to convert the date time object to a numeric value:

                        // compute the difference in days between 2 dates
                        // date1 - start date
                        // get field value
                        var sDate1 = this.getField('Text43').value;
                        //convert string to date time object - format picture is important
                        var oDate1 = util.scand('mm/dd/yyyy', sDate1);
                        // convert date time object to number - milliseconds since epoch date
                        var fDate1 = oDate1.valueOf();

                        // date 2 - end date
                        // get field value
                        var sDate2 = this.getField('Text51').value;
                        //convert string to date time object - format picture is important
                        var oDate2 = util.scand('mm/dd/yyyy', sDate2);
                        // convert date time object to number - milliseconds since epoch date
                        var fDate2 = oDate2.valueOf();

                        // compute difference of the values
                        var fDiff = fDate2 - fDate1;

                        // compute difference in days
                        var fDays = fDiff / (1000 * 60 * 60 * 24)
                        // get whole days only
                        var iDays = Math.floor(fDays)

                        // display difference in days for numeric field
                        event.value = iDays;

                        // some Debugging information
                        console.show();
                        console.clear();
                        console.println('Date1 value: ' + sDate1 + ' type of data: ' + (typeof sDate1) );
                        console.println('Object data of Date1: ' + oDate1 + ' type of data: ' + (typeof oDate1) );
                        console.println('Value of Date1 object: ' + fDate1 + ' type of data: ' + (typeof fDate1));

                        // difference in milliseconds
                        console.println('Difference of dates in milliseconds: ' + fDiff);
                        // difference in seconds
                        console.println('Difference in seconds: ' + (fDiff / 1000) );
                        // difference in minutes
                        console.println('Difference in minutes: ' + (fDiff / (1000 * 60)) );
                        // difference in hours
                        console.println('Difference in hours: ' + (fDiff / (1000 * 60 * 60)) );
                        // difference in days
                        console.println('Difference in days: ' + fDays);
                        console.println('Difference in whole days: ' + iDays);

                        This approach allows for full computation options from milliseconds to centuries and allows use of the result field's format to perform the formatting or allowing the user to format the result as a text string using the 'util.printd()' method.
                        • 9. Re: Javascript help
                          gkaiseril MVP & Adobe Community Professional
                          The following script can add 14 days to a provided date:

                          // add 14 days to a date
                          // date 1
                          // get the field value
                          var sDate1 = this.getField('Text43').value;
                          //convert string to date time object - format picture is important
                          var oDate1 = util.scand('mm/dd/yyyy', sDate1);
                          // convert date time object to number - milliseconds since epoch date
                          var fDate1 = oDate1.valueOf();
                          // one day in milliseonds
                          var fMDay = 1000 * 60 * 60 * 24;
                          // add 14 days to date
                          var fDate14 = fDate1 + (14 * fMDay);
                          // convert millisecond for date + 14 days to date time object
                          var oDate14 = new Date(fDate14);
                          // set field value using 'util.printd()' method
                          event.value = util.printd('m-d-yy', oDate14);

                          // some debugging information
                          console.println('Add 14 days to Date1');
                          console.println('Date1 in milliseonds: ' + fDate1);
                          console.println('Date1 + 14 days in milliseonds: ' + fDate14);
                          console.println('Oject for Date1 + 14 days: ' + oDate14);
                          • 10. Re: Javascript help
                            Level 1
                            I've figured out the script for adding 14 days but I'm still stuck on the other one. I'm not sure why I'm having such a hard time. I just put in this one that I was told would work and doesn't.

                            function Floor(fValue) {
                            return Math.floor(fValue);
                            }

                            function Date2Num(sDate, sFormat) {
                            var fSecond =1000;
                            var fMinute = 60 * fSecond;
                            var fHour = 60 * fMinute;
                            var fDay = 24 * fHour;
                            var oDate = util.scand(sFormat, sDate);
                            var fDate = oDate.valueOf();
                            var fDate = fDate / fDay;
                            var fDate = Floor(fDate);
                            return fDate;
                            }
                            var Start = this.getField("Text43").value;
                            var sFormat = "mm/dd/yyyy";
                            var End = this.getField("Text51").value;
                            var sFormat = "mm/dd/yyyy";
                            var eFormat = sFormat;
                            event.value = Date2Num(End, eFormat) - Date2Num(Start, sFormat);

                            P.S. I really appreciate everyone's help with this stuff since I am so clueless!
                            • 11. Re: Javascript help
                              gkaiseril MVP & Adobe Community Professional
                              In the 'custom calculation script' field the following works:

                              // start code
                              function Date2Num(sDate, sFormat) {
                              // some date time constants
                              var fSecond =1000; // milliseconds in a second
                              var fMinute = 60 * fSecond; //number of milliseconds in a minute
                              var fHour = 60 * fMinute; // number of milliseconds in an hour
                              var fDay = 24 * fHour; // number of milliseconds in a day
                              // convert passed date to date time object
                              var oDate = util.scand(sFormat, sDate);
                              // convert object to number
                              var fDate = oDate.valueOf();
                              return Floor(fDate / fDay);
                              }

                              // get start date and set format
                              var Start = this.getField("Text43").value;
                              var sFormat = "mm/dd/yyyy";
                              // get end date and set format
                              var End = this.getField("Text51").value;
                              var sFormat = "mm/dd/yyyy";
                              var eFormat = sFormat;
                              // compute the difference in days
                              event.value = Date2Num(End, eFormat) - Date2Num(Start, sFormat);

                              console.show(); console.clear();
                              console.println('Start: ' + Start + ' Format: ' + sFormat);
                              console.println('Start as days: ' + Date2Num(Start, sFormat) )
                              console.println('End: ' + End + ' Format: ' + eFormat);
                              console.println('End as days: ' + Date2Num(End, eFormat) );
                              console.println('Difference: ' + (Date2Num(End, eFormat) - Date2Num(Start, sFormat)) );

                              // end code

                              The 2 functions can be placed in the PDF's document scripts.
                              • 12. Re: Javascript help
                                Level 1
                                For some reason my version of the script is now working! Thanks again for everyone's help!
                                • 13. Re: Javascript help

                                  Sorry to jump in - I'm not a programmer and this is way beyond me. SO, please be patient.

                                   

                                  I need to add 7 days to an input date ("DATE"). I modified your script as follows and pasted it into the Custom Calculation script of the Calculate tab of the output date field ("DUEDATE"):

                                   

                                  // add 7 days to a date
                                  // date 1
                                  // get the field value
                                  var sDate1 = this.getField('DATE').value;
                                  //convert string to date time object - format picture is important
                                  var oDate1 = util.scand('mm/dd/yyyy', sDate1);
                                  // convert date time object to number - milliseconds since epoch date
                                  var fDate1 = oDate1.valueOf();
                                  // one day in milliseonds
                                  var fMDay = 1000 * 60 * 60 * 24;
                                  // add 7 days to date
                                  var fDate7 = fDate1 + (7 * fMDay);
                                  // convert millisecond for date + 7 days to date time object
                                  var oDate7 = new Date(fDate7);
                                  // set field value using 'util.printd()' method
                                  event.value = util.printd('mm/dd/yyyy', oDate14);

                                  // some debugging information
                                  console.println('Add 7 days to Date1');
                                  console.println('Date1 in milliseonds: ' + fDate1);
                                  console.println('Date1 + 7 days in milliseonds: ' + fDate7);
                                  console.println('Oject for Date1 + 7 days: ' + oDate7);

                                   

                                  However, my output field ("DUEDATE") remains blank after I fill in the DATE field. Any help you can provide will be greatly appreciated.

                                  • 14. Re: Javascript help
                                    Level 1

                                    Sorry - I am using Adobe Acrobat Pro 9.

                                    • 15. Re: Javascript help
                                      gkaiseril MVP & Adobe Community Professional

                                      Do you get any messages on the JavaScript debugging console?

                                       

                                      The following code sets the value of the field and the variable 'oDate14' is not initialized nor set to any value:

                                       

                                      // set field value using 'util.printd()' method
                                      event.value = util.printd('mm/dd/yyyy', oDate14);

                                       

                                      I get the following messages:

                                       

                                      oDate14 is not defined
                                      16:Field:Calculate
                                      oDate14 is not defined
                                      16:Field:Calculate
                                      oDate14 is not defined
                                      16:AcroForm:DATEPLUS7:Calculate
                                      oDate14 is not defined
                                      16:AcroForm:DATEPLUS7:CalculateAdd 7 days to Date1

                                       

                                       

                                      And the result field is not filled in because JavaScript has encountered and error and stopped.

                                       

                                      When the variable 'oDate14' is changed to 'oDate7' I get the following messages:

                                       

                                      Date1 in milliseonds: 1240345875481
                                      Date1 + 7 days in milliseonds: 1240950675481
                                      Oject for Date1 + 7 days: Tue Apr 28 2009 15:31:15 GMT-0500 (Central Daylight Time)

                                      And the field poperly fills in.

                                      • 16. Re: Javascript help
                                        Level 1

                                        Thank you for the pointer - I missed changing the name of that last variable. It works now.