Skip to main content
Participating Frequently
December 15, 2016
Answered

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

  • December 15, 2016
  • 1 reply
  • 1530 views

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

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

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);

1 reply

Inspiring
December 15, 2016

Have you checked the JavaSvript console for any errors?

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

Participating Frequently
December 15, 2016

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?

Inspiring
December 15, 2016

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.