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

Default Date on a Form Question

trefilov22
Registered: Jun 29 2007
Posts: 39

I am creating a form, and want to have the date/time automatically input into the form, which I have done. I want the form only to update the date and time on a save/modify of the document. I have accomplished this as well. The problem is this, when I want a user to start a new form, it automatically puts the date that the form was created in as the date, which confuses the user. I want it to put the current date/time in, when the clean form is opened (ie opened as a fresh form with no entries).

Any suggestions? Thanks in advance!

B

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Havw you read:

http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=431

George Kaiser

trefilov22
Registered: Jun 29 2007
Posts: 39
I have, and have managed to get it to input the date and time correctly upon a save, but on the original, blank form (the one people will be filling out) it keeps the time from when I originally created the form as the date and time. I would like that to put in the current date on the initial use of the form, and then only update the date and time if the form is edited in the future.
trefilov22
Registered: Jun 29 2007
Posts: 39
Oh and I appreciate your help too, thanks! :)
scottsheck
Registered: May 3 2007
Posts: 138
Are you seeing the date you created the form when you open the form up in Acrobat or Reader?

If it's reader, then you simply need to clear the field just before you save it from Acrobat.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The following script used as dcoument level script will overwite whatever was previously saved:

// clear default value for fields before saving file for distribution

function ClearDateTimeDefault() {
this.getField("NowDate").defaultValue = "";
this.getField("NowTime").defaultValue = "";
this.getField("NowDateTime").defaultValue = "";
return;
} // end ClearDateTimeDefault function

ClearDateTimeDefault(); // clear date time fields for initialization

var oDateTime = new Date(); // get date time object

// initialize a date field
var NowDate = this.getField("NowDate");
if (NowDate.defaultValue == "") // update if default value empty
NowDate.value = util.printd("mmm dd, yyyy ddd", oDateTime); // set format string to field's format
NowDate.defaultValue = NowDate.value; // prevent loss of data on reset

// intialize a time field
var NowTime = this.getField("NowTime");
if (NowTime.defaultValue == "") // update time if default empty
NowTime.value = util.printd("h:MM:ss tt", oDateTime); // set format string to field's format
NowTime.defaultValue = NowTime.value; //prevent loss of data on reset

// initialize a date/time field
var NowDateTime = this.getField("NowDateTime");
if (NowDateTime.defaultValue == "")
NowDateTime.value = util.printd("ddd mmm dd, yyyy h:MM:ss tt", oDateTime); // set format string to field's format
NowDateTime.defaultValue = NowDateTime.value; // prevent loss of data on reset

George Kaiser

trefilov22
Registered: Jun 29 2007
Posts: 39
scottsheck wrote:
Are you seeing the date you created the form when you open the form up in Acrobat or Reader?If it's reader, then you simply need to clear the field just before you save it from Acrobat.
Exactly, when I open the form in Acrobat or Reader, I see the date I created the form. I would rather it open with the current date, UNLESS its been modified (ie someone actually filled the form out and saved). The people filling in the form use Acrobat standard, not the reader.
scottsheck
Registered: May 3 2007
Posts: 138
Then you simply create a text field, and enter the function in it's calculate event with an 'if not filled in, then fill it with the current date' statement.

They key is that you clear out the contents of the text field manually just before you save it. THen when the next person opens it up, it will fill in the current date, rather than leave the last date it had calculated. It definately works, I do it all the time.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
If the script is in the calculate event, it will update with change of any field used in a calculation. That is why I use the document level JavaScript. There are also methods that can be used to prevent the changing of the date in a saved PDF.

George Kaiser

trefilov22
Registered: Jun 29 2007
Posts: 39
gkaiseril wrote:
The following script used as dcoument level script will overwite whatever was previously saved:// clear default value for fields before saving file for distribution

function ClearDateTimeDefault() {
this.getField("NowDate").defaultValue = "";
this.getField("NowTime").defaultValue = "";
this.getField("NowDateTime").defaultValue = "";
return;
} // end ClearDateTimeDefault function

ClearDateTimeDefault(); // clear date time fields for initialization

var oDateTime = new Date(); // get date time object

// initialize a date field
var NowDate = this.getField("NowDate");
if (NowDate.defaultValue == "") // update if default value empty
NowDate.value = util.printd("mmm dd, yyyy ddd", oDateTime); // set format string to field's format
NowDate.defaultValue = NowDate.value; // prevent loss of data on reset

// intialize a time field
var NowTime = this.getField("NowTime");
if (NowTime.defaultValue == "") // update time if default empty
NowTime.value = util.printd("h:MM:ss tt", oDateTime); // set format string to field's format
NowTime.defaultValue = NowTime.value; //prevent loss of data on reset

// initialize a date/time field
var NowDateTime = this.getField("NowDateTime");
if (NowDateTime.defaultValue == "")
NowDateTime.value = util.printd("ddd mmm dd, yyyy h:MM:ss tt", oDateTime); // set format string to field's format
NowDateTime.defaultValue = NowDateTime.value; // prevent loss of data on reset
OK I am having issues getting this to work. I am guess the NowDateTime is the name of the field that we want the time/date to be entered in to? I am new to this and still trying to figure it out. Thanks again for all the help though, its appreciated.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Yes, anywhere there is a "this.getField()" statement one is identifying a field, and when assigned to a variable, that variable is the same as the field. This code is also written for Acrobat Forms, but there should be equivalents available to LiveCycle Designer.

George Kaiser

trefilov22
Registered: Jun 29 2007
Posts: 39
OK cool, I will try to fiddle around with it. Thanks for the quick response.
trefilov22
Registered: Jun 29 2007
Posts: 39
gkaiseril wrote:
Yes, anywhere there is a "this.getField()" statement one is identifying a field, and when assigned to a variable, that variable is the same as the field. This code is also written for Acrobat Forms, but there should be equivalents available to LiveCycle Designer.
Also I am using Acrobat Professional 8.0, and not using the livecycle portion.
trefilov22
Registered: Jun 29 2007
Posts: 39
Also you have references to "NowDate", "NowTime", "NowDateTime". Which one of those should I change. I only have one text entry where the date and time both enter. Should I create 3 different text entries with those names?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Yes, the code is for Acrobat Forms and not LiveCycle Designer. If you want a date and time field only:

// clear default value for fields before saving file for distribution

function ClearDateTimeDefault() {
this.getField("NowDateTime").defaultValue = "";
return;
} // end ClearDateTimeDefault function

ClearDateTimeDefault(); // clear date time fields for initialization

var oDateTime = new Date(); // get date time object

// initialize a date/time field
var NowDateTime = this.getField("NowDateTime");
if (NowDateTime.defaultValue == "")
NowDateTime.value = util.printd("ddd mmm dd, yyyy h:MM:ss tt", oDateTime); // set format string to field's format
NowDateTime.defaultValue = NowDateTime.value; // prevent loss of data on reset

You may also be interested in the following articles:
http://www.planetpdf.com/developer/article.asp?ContentID=displaying_the_current_date&gid=6197 Displaying the current Date
http://www.planetpdf.com/developer/article.asp?ContentID=6828 Example Acrobat JavaScripts

George Kaiser

trefilov22
Registered: Jun 29 2007
Posts: 39
gkaiseril wrote:
Yes, the code is for Acrobat Forms and not LiveCycle Designer. If you want a date and time field only:// clear default value for fields before saving file for distribution

function ClearDateTimeDefault() {
this.getField("NowDateTime").defaultValue = "";
return;
} // end ClearDateTimeDefault function

ClearDateTimeDefault(); // clear date time fields for initialization

var oDateTime = new Date(); // get date time object

// initialize a date/time field
var NowDateTime = this.getField("NowDateTime");
if (NowDateTime.defaultValue == "")
NowDateTime.value = util.printd("ddd mmm dd, yyyy h:MM:ss tt", oDateTime); // set format string to field's format
NowDateTime.defaultValue = NowDateTime.value; // prevent loss of data on reset

You may also be interested in the following articles:
http://www.planetpdf.com/developer/article.asp?ContentID=displaying_the_current_date&gid=6197 Displaying the current Date
http://www.planetpdf.com/developer/article.asp?ContentID=6828 Example Acrobat JavaScripts
If I am in forms, where exactly do I put this code. Under the:

Advanced > Document Processing > Document Javascripts?Thanks again!

B
trefilov22
Registered: Jun 29 2007
Posts: 39
gkaiseril wrote:
Yes, the code is for Acrobat Forms and not LiveCycle Designer. If you want a date and time field only:// clear default value for fields before saving file for distribution

function ClearDateTimeDefault() {
this.getField("NowDateTime").defaultValue = "";
return;
} // end ClearDateTimeDefault function

ClearDateTimeDefault(); // clear date time fields for initialization

var oDateTime = new Date(); // get date time object

// initialize a date/time field
var NowDateTime = this.getField("NowDateTime");
if (NowDateTime.defaultValue == "")
NowDateTime.value = util.printd("ddd mmm dd, yyyy h:MM:ss tt", oDateTime); // set format string to field's format
NowDateTime.defaultValue = NowDateTime.value; // prevent loss of data on reset

You may also be interested in the following articles:
http://www.planetpdf.com/developer/article.asp?ContentID=displaying_the_current_date&gid=6197 Displaying the current Date
http://www.planetpdf.com/developer/article.asp?ContentID=6828 Example Acrobat JavaScripts
Ok I tested this out but it appears that the time updates everytime you open the document. What I would like to do is once the document is saved, the time doesn't auto update unless its saved again. So if it saves originally at 9:11am, if you send this to someone and they open it at 10, it still says 9:11. I'm pulling my hair out! :)
scottsheck
Registered: May 3 2007
Posts: 138
so have you tried my suggestion. That is, when you are about to distribute the form with no entries, clear out that date field by hand, and save the form from the acrobat menu.

I don't want to interfere with gkaiseril's great help though, but just curious why what I mentioned doesn't work.
trefilov22
Registered: Jun 29 2007
Posts: 39
scottsheck wrote:
so have you tried my suggestion. That is, when you are about to distribute the form with no entries, clear out that date field by hand, and save the form from the acrobat menu.I don't want to interfere with gkaiseril's great help though, but just curious why what I mentioned doesn't work.
Yes I have, and it will save the date right back in as soon as I choose to save it. Its just a frustrating problem.
scottsheck
Registered: May 3 2007
Posts: 138
If you are really desperate, here is one approach that's not the most elegant, but will work. You can create a global variable that you start at zero. In your save routine, you increment that variable by one each time.

Then you also only fill in the date if that variable is greater than zero. Designer has global form variables, but I'm not sure if Acrobat forms does. If not, you could just use a hidden field for a counter.
trefilov22
Registered: Jun 29 2007
Posts: 39
Is there a way to make it so that if a different field is empty (which is the default) then it will reset the date field to the current date? Then if this 2nd field has an item in it, it will not update the date field until saved?
scottsheck
Registered: May 3 2007
Posts: 138
The 2nd field approach would have the same problem that you're currently having. In other words, if you could have accomplished this with a 2nd field, then you could have accomplished it with just your one date field.

I would try the global variable counter, that would definately work.
trefilov22
Registered: Jun 29 2007
Posts: 39
scottsheck wrote:
The 2nd field approach would have the same problem that you're currently having. In other words, if you could have accomplished this with a 2nd field, then you could have accomplished it with just your one date field.I would try the global variable counter, that would definately work.
I'm not sure how to do that...
scottsheck
Registered: May 3 2007
Posts: 138
2 steps:

1) From designer, go to File, Form properties, then the variables tab. add a variable called 'counter' and set the value to 0 (zero).

2) in the code you have to save the date into that date field when a user saves the form, precede it with something like this (I used formcalc syntax below):

if (counter == 0) then
...Set that date field to blank
else
...Set the date field to the current date
endif
counter = counter + 1



so now, when you distribute a form for the first time, set the counter variable in the form, properties is set to zero.
trefilov22
Registered: Jun 29 2007
Posts: 39
OK now we're thinking about heading a different direction with this. Is there a way to have the user pick a date to put in there from say, a calendar or something, rather than the date automatically being applied? If so, how could I go about doing that?
scottsheck
Registered: May 3 2007
Posts: 138
Why not just do both. That's exactly what I do in my forms. If the date is blank, it is automatically filled in with the current date, but the user can always modify to. Just by making the field a date field, it automatically will pull up a calendar.
trefilov22
Registered: Jun 29 2007
Posts: 39
scottsheck wrote:
Why not just do both. That's exactly what I do in my forms. If the date is blank, it is automatically filled in with the current date, but the user can always modify to. Just by making the field a date field, it automatically will pull up a calendar.
Ok how would I make it a date field? What you are saying sounds like what I want to do.
scottsheck
Registered: May 3 2007
Posts: 138
The same way you change a field from numeric, to text, to dropdown, etc. The type is called Date/Time field. You must not be using Designer?
trefilov22
Registered: Jun 29 2007
Posts: 39
scottsheck wrote:
The same way you change a field from numeric, to text, to dropdown, etc. The type is called Date/Time field. You must not be using Designer?
No I am just editing an existing form in Acrobat, thats probably the problem. I changed the format of the text entry to date, but it doesn't fill in anything. Acrobat Professional 8 is what I am using. I'm not familiar with designer, but could use LiveCycle if need be I suppose.
trefilov22
Registered: Jun 29 2007
Posts: 39
Got the calendar option to work in LiveCycle, thanks for the help!
trefilov22
Registered: Jun 29 2007
Posts: 39
gkaiseril wrote:
The following script used as dcoument level script will overwite whatever was previously saved:// clear default value for fields before saving file for distribution

function ClearDateTimeDefault() {
this.getField("NowDate").defaultValue = "";
this.getField("NowTime").defaultValue = "";
this.getField("NowDateTime").defaultValue = "";
return;
} // end ClearDateTimeDefault function

ClearDateTimeDefault(); // clear date time fields for initialization

var oDateTime = new Date(); // get date time object

// initialize a date field
var NowDate = this.getField("NowDate");
if (NowDate.defaultValue == "") // update if default value empty
NowDate.value = util.printd("mmm dd, yyyy ddd", oDateTime); // set format string to field's format
NowDate.defaultValue = NowDate.value; // prevent loss of data on reset

// intialize a time field
var NowTime = this.getField("NowTime");
if (NowTime.defaultValue == "") // update time if default empty
NowTime.value = util.printd("h:MM:ss tt", oDateTime); // set format string to field's format
NowTime.defaultValue = NowTime.value; //prevent loss of data on reset

// initialize a date/time field
var NowDateTime = this.getField("NowDateTime");
if (NowDateTime.defaultValue == "")
NowDateTime.value = util.printd("ddd mmm dd, yyyy h:MM:ss tt", oDateTime); // set format string to field's format
NowDateTime.defaultValue = NowDateTime.value; // prevent loss of data on reset
Question regarding this gkaiseril, if the function reruns on each startup, wouldn't it reset the default value to "" each time, thus causing the date/time to automatically update? I know at the end of the script it says to change the default value to NowDateTime.value but when you open back up the form wouldn't the function ClearDateTimeDefault() overwrite that? Let me know what you think.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Yes it does, but if one were to reset the form fields by a JavaScript or the menu option, the date/time would not be changed. But one could add some conditional code to test if the defalult value is empty and then update the date time field values. One could also use a "Will Save" action to clear the default value and then remove the "Will Save" action allowing for the appliction of extended reader rights.

George Kaiser

trefilov22
Registered: Jun 29 2007
Posts: 39
Gotcha, I am just too new at this to know what the heck to do in a situation like that. This is my first experience with JavaScript in Adobe. I have limited experience with it in websites and such, but not even a lot there either.