Exactly how you do it depends on when you want the field to be populated. Is it when the document opens, when a button is pressed, or when something else happens?
Create a document-level JavaScript (Advanced > Document Processing > Document JavaScripts) and add the following code outside of a function definition.// Set field value to current system date getField("your_date_field").value = util.printd("yyyy-dd-mm", new Date());
The code will execute when the document is opened, replacing the previous field value. Use the actual text field name and whatever date format code you want. For more info, see: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1251.php
I just noticed you're using Acrobat 10. The way to add a document-level JavaScript is different, but I can't check at the moment to say exactly how.
- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com
The 'util.printd()' method formats the date object of JavaScript. You need to add extra code to set the value and default value of a given field.
// set a variable for the field name to populate with the current date var sField = "My Auto Date Field Name"; // set a variable for the dispaly format of the current date var sFormat = "mmm dd, yyyy"; // do not change the code below this line
// set field value with the specified format this.getField(sField).value = util.printd(sFormat, new Date() ); // set default value for the field so a form reset will not clear the value this.getField(sField).defaultValue = this.getField(sField).value;
It is also possible to autofill the date using one line of code:
1) Double click or right click on your date "text box" and enter the following as a "Custom calculation script" in the text field properties (under the Calculate tab):
this.getField("YourFieldName").value = new Date;
2) While in the text field properties box, select Format category "Date" to display the date in your desired format.
I may take a couple of more lines, but the overhead is far less than having the code in the calculation script run each time any field of the form performs a calculation. And yes I can make my code shorter, but some answers are also providing additional instruction on how to write more complex scripts or more detail about how a script works. If you wanted to you can remove 'this' and save 5 key strokes. Also my code maybe used in newer versions of Acrobat/Reader that might edit the code more strictly than the current version.