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

Autofill date

bbelich
Registered: Nov 29 2010
Posts: 17
Answered

I cannot figure out how to autofill the date, with today's date, in a text box.

Bob

My Product Information:
Acrobat Pro 10.0, Windows
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
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?
bbelich
Registered: Nov 29 2010
Posts: 17
When the document is opened.

Bob

George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
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.
bbelich
Registered: Nov 29 2010
Posts: 17
Thank You for the script.
I found out how to create the Document-level script but i couldn't figure out the actual script.

Thanks again for your help.
It is much appreciated.

Bob

try67
Expert
Registered: Oct 30 2008
Posts: 2398
What's the name of the field in question?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
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;

George Kaiser

hher411
Registered: Oct 6 2010
Posts: 3
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.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
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.

George Kaiser