11 Replies Latest reply on Dec 15, 2016 2:04 PM by -MaddAddaM-

    Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"

    -MaddAddaM- Level 1

      Hi,

      I have two text boxes in a PDF form: "Month Ended" and "Due Date." The user is supposed to enter a month end date in the "Month Ended" text box. This is validated to be a "mm/dd/yyyy" format.

       

      When the user enters a date in "Month Ended" I want to auto-calculate the "Due Date" field. It should always be the last day of the next month and account for leap years. For example, if I put in 08/31/2016 (Month Ended text box) it should return 09/30/2016 (Due Date text box). Or if I put in 01/31/2016 it should return 02/29/2016 (since we have a leap year this year). Please note I also tried to account for the scenario where a user might not follow directions and enter a "month end" date (i.e. would instead enter the middle of the month, etc.).

       

      I thought Datejs would work but I can't seem to get it to work. Here is the script I have in "Custom Calculation script" field of the "Due Date" text box:

       

      var MonthEnded = thisgetField("Month Ended").value;

      MonthEnded.moveToLastDayOfMonth();

      MonthEnded.addMonths(1);

      MonthEnded.moveToLastDayofMonth();

      event.value = util.printd("mm/dd/yyyy",MonthEnded");

       

      Can anyone help me accomplish what I'm trying to do?

      Thanks,

      A

        • 1. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
          gkaiseril MVP & Adobe Community Professional

          Have you checked the JavaSvript console for any errors?

           

          You have a number of syntax errors and an undefined function.

          • 2. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
            -MaddAddaM- Level 1

            I'm new to this. Sorry. I'm not sure how I do that. If you see issues with the code, can you tell me what you would do to correct them?

            • 3. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
              gkaiseril MVP & Adobe Community Professional

              You can bring up the Acrobat JavaScript Console using the key combination of <Ctrl> + "J" keys.

               

              var MonthEnded = thisgetField("Month Ended").value;

               

              Has a syntax error. You have not indicated made clear what the object and method you want to use. "getField" is a method of the document or doc object. An object is separated from the method to be used by a ".". It should read:

               

              var MonthEnded = this.getField("Month Ended").value;

               

              There is no predefined or provided method or function "moveToLastDayofMonth()".

               

              It is possible to get the last day of a month from a date object. It takes some additional lines of code to advance the date object to the first of the month 2 months from the date entered and then backup one day.

               

              You also did not specify the action where you placed the script. If one were to place the script in the "Custom JavaScript" calculation option it would run and adjust the field's value each time a calculated field was updated. I do not think this is what you want to happen. You should only want the script to run when the field is updated.

               

               

               

               

              • 4. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                Karl Heinz Kremer Adobe Community Professional

                There are a number of problems with your code. Let's first talk about the syntax errors:

                This is the code as you've quoted it:

                 

                var MonthEnded = thisgetField("Month Ended").value;
                MonthEnded.moveToLastDayOfMonth();
                MonthEnded.addMonths(1);
                MonthEnded.moveToLastDayofMonth();
                event.value = util.printd("mm/dd/yyyy",MonthEnded");
                

                 

                In line 1, you need a period between "this" and "getField".

                In line 5, there is an extra double-quote just before the final ")", this needs to be removed. These two problems are being reported on the JavaScript console, which you can bring up using Ctrl-J or Cmd-J. After these two changes, you should not get any simple syntax errors anymore. Now we are talking about errors in how you programmed this snippet.

                 

                The biggest problem is that you are trying to reference the DateJS library, which is not available in Acrobat. It's actually not even available when you create JavaScript for a web page - unless you include it in your code. You might be able to do this in Acrobat as well, but this is not what I would recommend - unless you want to spend the time required to make sure that you are not running into any strange side effects.

                 

                To include this library, download it from http://www.datejs.com/

                 

                The best way to add it to your system is to create a folder level script - this way you can make sure that it only gets included once. If that is not possible because you are trying to distribute your document outside of your organization, then the next best thing is to create a document level script (e.g. call it DateJS) and add the whole library. Now you should be able to use in in your code. But that will bring up other problems:

                 

                JavaScript is case sensitive, so you need to fix the case of the "o" in moveToLastDayofMonth - make it moveToLastDayOfMonth. However, you can only use this on a Date object that was modified by DateJS - to create a date object, you need to create it via the "new" operator. Here is the fixed code (which again, will only work if you include DateJS):

                 

                var MonthEnded = new Date(this.getField("Month Ended").value);
                MonthEnded.moveToLastDayOfMonth();
                MonthEnded.addMonths(1);
                MonthEnded.moveToLastDayOfMonth();
                event.value = util.printd("mm/dd/yyyy", MonthEnded);
                
                • 5. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                  Joel_Geraci Adobe Community Professional

                  Karl:

                   

                  I tried adding date.js to Acrobat as-is but not everything worked as I would have expected so I made some modifications and additions and your timing couldn't be better.

                   

                  I'm a couple of days away from publishing my own Date Library for Acrobat that does a lot of the stuff that date.js does as well as some of the stuff that PHP Carbon does. It also adds the util.scand functionality into the Mobile JS interpreter. It'll be distributed as a custom command that inserts the script into the document. Totally free to use, distribute, and modify.

                   

                  If anyone on this thread is interested in testing it with me, contact me directly.

                   

                   

                   

                  J-

                  • 6. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                    Karl Heinz Kremer Adobe Community Professional

                    Joel,

                     

                    thanks of the heads up. All I did was to add the library to a document level script and check for error messages on the console. The functionality used in the script above does work, but again, I don't know about any undesired side effects, or other areas in DateJS. I am glad to hear that you are working on something to provide more functionality to Acrobat's date processing.

                    • 7. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                      -MaddAddaM- Level 1

                      Thank you for your clarity! Is "Load(Resource("Date.JS").value);" the appropriate call to add the whole library under "Document Javascripts?"

                      • 8. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                        Karl Heinz Kremer Adobe Community Professional

                        You are thinking scripts for web browsers, that is not what you are dealign with here: Acrobat's JavaScript implementation is different.

                         

                        All you need to do is to select the Document JavaScripts tool in the JavaScript tools group, and then specify a name and click on the "Add" button:

                         

                        2016-12-15_13-39-25.png

                         

                        2016-12-15_13-41-01.png

                         

                        Keep in mind that - as Joel indicated - not all functionality of DateJS will work correctly, and we don't even know if what you are trying to do will work in all cases.

                        • 9. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                          -MaddAddaM- Level 1

                          I get there, but I'm not sure the syntax to use to actually load it. Below is what I get:

                           

                           

                          It's creating a function, but I'm not sure how to permanently load the library. Do I just call it from where it is sitting on my computer?

                          • 10. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                            Karl Heinz Kremer Adobe Community Professional

                            You paste the complete code of the library into the editor window - replacing everything that's there - after you click on the Add button.

                            • 11. Re: Datejs Help: Move Date in "Textbox1" to Next Month End in "Textbox2"
                              -MaddAddaM- Level 1

                              That worked. I really appreciate your help. Thank you for taking the time to answer my questions even though they were probably very elementary. It was very kind.