7 Replies Latest reply on Apr 20, 2015 8:04 AM by gkaiseril

    Working with date and time in acrobat JavaScript help request...

    JohnnyBQue

      This came from

      Working with date and time in Acrobat JavaScript (Part 1 of 3)

      The following code adds five days to the current date and then prints the new date to the console window.

       

      // Get the current date and time

      var rightNow = new Date();

       

      // Get the millisecond value

      // of the date object

      var msRightNow = rightNow.getTime();

       

      // Calculate 5 days in milliseconds,

      // 5 days x 24 hrs/day x 60 min/hr x 60 sec/min x 1000 ms/sec

      var fiveDays = 5 * 24 * 60 * 60 * 1000;

       

      // Do Calculation var finalTime = msRightNow + fiveDays;

      // Create a new Date from the calculated value

      var theNewDate = new Date(finalTime);

       

      // In actual working code on a form you'll want to place the

      // result into a field value. But since this is test code we'll

      // Print the result to the Console Window (Great for Debugging)

      console.println("5 days from now is: " + theNewDate.toString());

       

      The above is the exact code (cut&paste)

       

      The Error I get here is;

      ReferenceError: theNewDate is not defined

      1:Console:Exec

      undefined

       

      I seem to get the "not defined" issue a lot...learning how to distinguish between the  undefined "type" undefined "variable" and undefined "value"

      in this case "theNewDate" seems to be defined as a variable...equal to the current date plus the 5 days.... BUT I'm now "schooled" enough to "see" why I'm get the error....


      Any thoughts....Anyone.....

        • 1. Re: Working with date and time in acrobat JavaScript help request...
          gkaiseril MVP & Adobe Community Professional

          for the code:

           

          // Do Calculation var finalTime = msRightNow + fiveDays;

          // Create a new Date from the calculated value

          var theNewDate = new Date(finalTime);

           

           

          what is the value of "finalTime"?

          Where is it defined?

          How is it calculatied?

           

          I would look at using the "setDate" method to calculate the date and time 5 days into the future by adding 5 days to the result obtained using the "getDate" method.

           

          Try:

           

          // Get the current date and time

          var rightNow = new Date();

          console.println("now is: }" + rightNow);

          // add 5 days to the date value of the date object;

          var theNewDate = rightNow;

          theNewDate.setDate(rightNow.getDate() + 5);

          // Print the result to the Console Window (Great for Debugging)

          console.println("5 days from now is: " + theNewDate);

          • 2. Re: Working with date and time in acrobat JavaScript help request...
            hasand59089870 Level 2

            wrong:

            // Do Calculation var finalTime = msRightNow + fiveDays;

             

            right:

            var finalTime = msRightNow + fiveDays;

            • 3. Re: Working with date and time in acrobat JavaScript help request...
              JohnnyBQue Level 1

              Now I took the code and deleted the "comment" and it ends up looking like this

               

              var rightNow = rightNow.getTime();

              var fiveDays = 5 * 24 * 60 * 60 * 1000;

              var finalTime = msRightNow + fiveDays;

              var theNewDate = new Date(finalTime);

              console.println("5 days from now is: " + theNewDate.toString());

               

               

              Now if I place the cursor and the end of the code and pressed the "enter" button from the keypad area of my key board I kept getting the error mentioned in the original post above

               

              BUT when I went to the end of each line with the cursor and pressed the enter button I ended up getting.

               

              undefined

              undefined

              undefined

              undefined

              undefined5 days from now is: Wed Apr 22 2015 12:55:44 GMT-0400 (Eastern Daylight Time)

               

              true

               

              NOW ever since I went line for line in the code, pressing the enter button, if I place it at the end of the code and press the enter button I get...

               

              5 days from now is: Wed Apr 22 2015 12:55:44 GMT-0400 (Eastern Daylight Time)

               

              true

               

              The code seem to work fine now every time I execute it... was it me just not knowing how to use the debugger properly?

               

              opinions?.....

              • 4. Re: Working with date and time in acrobat JavaScript help request...
                gkaiseril MVP & Adobe Community Professional

                You need to highlight all of the text you want to have run.

                 

                You have not defined the rightNow variable before you can apply a method to the variable.

                 

                You cannot just add a new variable name like msRightNow. Why not use rightNow.

                 

                Running the code you provided with a fresh start of Acrobat, I get the following error messages:

                 

                rightNow.getTime is not a function

                1:Console:Exec

                TypeError: rightNow.getTime is not a function

                1:Console:Exec

                undefined

                 

                What you are showing probably works because your prior attempts have left behind a variable "rightNow" and as long as you do not have the date time object change by too much, you might not notice the error.


                A cleanup of your code:

                 

                // get the number of milliseconds from the current date object;

                var rightNow = (new Date()).getTime();

                // define 5 days in milliseconds;
                var fiveDays = 5 * 24 * 60 * 60 * 1000;

                // add 5 days to the value of right now and assign the result to final time;
                var finalTime = rightNow + fiveDays;

                // convert the finalTime value to a date object;
                var theNewDate = new Date(finalTime);

                // display the result;
                console.println("5 days from now is: " + theNewDate.toString());

                 

                Did you try the code I provided, It is a different way of adding days to the date time object.

                • 5. Re: Working with date and time in acrobat JavaScript help request...
                  JohnnyBQue Level 1

                  Acrobat EScript Built-in Functions Version 10.0

                  Acrobat SOAP 10.0

                   

                  (The text in BOLD is the CODE you provided...the text in ITALIC is the results for that line of code after hitting the keypad "enter" button)

                   

                  // Get the current date and time

                  var rightNow = new Date();      >>>>>>> undefined

                  console.println("now is: }" + rightNow);      >>>>>>>> now is:  }Mon Apr 20 2015 07:50:34 GMT-0400 (Eastern Daylight Time)        true

                  // add 5 days to the date value of the date object;

                  var theNewDate = rightNow;      >>>>>>>>> undefined

                  theNewDate.setDate(rightNow.getDate() + 5);     >>>>>>>> 1429959934561

                  // Print the result to the Console Window (Great for Debugging)

                  • 6. Re: Working with date and time in acrobat JavaScript help request...
                    JohnnyBQue Level 1

                    This worked fine...

                     

                    // get the number of milliseconds from the current date object;

                    var rightNow = (new Date()).getTime();

                    // define 5 days in milliseconds;
                    var fiveDays = 5 * 24 * 60 * 60 * 1000;

                    // add 5 days to the value of right now and assign the result to final time;
                    var finalTime = rightNow + fiveDays;

                    // convert the finalTime value to a date object;
                    var theNewDate = new Date(finalTime);

                    // display the result;
                    console.println("5 days from now is: " + theNewDate.toString());

                    • 7. Re: Working with date and time in acrobat JavaScript help request...
                      gkaiseril MVP & Adobe Community Professional

                      This what I get when I run it:

                       

                      Acrobat JavaScript Debugger Functions Version 11.0
                      Acrobat EScript Built-in Functions Version 11.0
                      Acrobat SOAP 11.0

                       

                      // Get the current date and time

                      var rightNow = new Date();

                      console.println("now is: }" + rightNow);

                      // add 5 days to the date value of the date object;

                      var theNewDate = rightNow;

                      theNewDate.setDate(rightNow.getDate() + 5);

                      // Print the result to the Console Window (Great for Debugging)

                      console.println("5 days from now is: " + theNewDate);

                       

                      now is: }Mon Apr 20 2015 10:03:30 GMT-0500 (Central Daylight Time)
                      5 days from now is: Sat Apr 25 2015 10:03:30 GMT-0500 (Central Daylight Time)

                      true