These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Updating a Year Calculation

1stRita
Registered: Feb 29 2008
Posts: 5
Answered

I created a form last year with two "Year" fields. I received help on forum with custom calculation scripts to update the year to the current year in each of the fields. "Year1" needs to be the year that ended on 12/31 and "Year2" needs to be the current year. They need to update to the appropriate years on each new year. The custom calculation in "Year1" is:
// using the getFullYear() method:
event.value = new Date().getFullYear() - 1
and in "Year2" is:
var year=new Date().getFullYear();
event.value = year
Apparently the scripts did not update as expected. Would sure appreciate any assistance to fix these fields or please let me know if this is not something that can even work the way we would like. Thanks in advance for your help!

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The scripts are probably OK, but they need to be triggered in order for the change to occur. They should be triggered when any field value in the form changes, assuming there are other fields and that their values can change.

A better location for the code would be in a document-level JavaScript (outside of a function) so that the code is executed every time the document is opened.
1stRita
Registered: Feb 29 2008
Posts: 5
Would I just copy and paste the same code into he Action area, "Run a JavaScript" on Mouse Up? Sorry, I am unsure of what exactly to do with the scripts? Thank you for your response.
1stRita
Registered: Feb 29 2008
Posts: 5
Okay...I see that the "Year1" does, in fact update to last year (ending 12/31) when another field is changed, as you said. That would be fine, although somewhat confusing if the users don't know that it will do that. It would be better the way that you mentioned, George, if you wouldn't mind helping me get there. Oh and..."Year2" field is not updating to the current year at all, it is still at 2010.
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
If you moved the code to a document-level script, it would look like:

// Document-level JavaScript
getField("Year1").value = new Date().getFullYear() - 1;
getField("Year2").value = new Date().getFullYear();


And remove the current code in those fields' Calculate event.

1stRita
Registered: Feb 29 2008
Posts: 5
Thanks so much. Is there anyway to check it or just wait until the year changes again?
1stRita
Registered: Feb 29 2008
Posts: 5
Made the suggested changes and Year1 field is correct (shows last year) but Year2 field also shows last year. Any suggestions?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
To check, you can change the system date on your computer. I don't know why the other field is not updating correctly. Could it be that something else is setting the field value?