Fill Acrobat Form date field with today's date only if field is empty
Asked Answered
A

4

5

The goal is for an Acrobat form to fill a date field ("MeetingDate") with Today's date when the user opens it. If the user saves the form as is and opens it a few days later, the old date should still be there, not replaced by the current date. If the user changes date in the field, that date should be saved and should not be replaced when the form is opened later.

I have a Custom Calculation Script for a date field ("MeetingDate") that does all of this, but with one problem:

if (event.value != "")
then
event.value = util.printd ("m/d/yyyy", new Date())
endif

This works well except that after entering the script, today's date fills in the MeetingDate field and the value is saved. That makes sense, but I want the field to be automatically filled with Today's date when the user opens the form. Instead, if the user opens the form tomorrow (7/16/2010) it will have 7/15/2010 in the field because that is the date I saved the form.

I think the answer may be to enter a script as a Document JavaScript (Advanced | Document Process | Document JavaScripts), since Document scripts execute when the form is opened, but all my attempts cause a date entered by the user to be overwritten when the form is opened days later. Thanks for the help!

Afebrile answered 16/7, 2010 at 3:22 Comment(1)
About your script: event.value refers to the value of an event (has nothing to do with the value of your field). It will always fire, and your clause will always be true. That's why the field is always reset. See my answer below.Trophoplasm
T
11

Remove your script and replace it with the following document javascript:

var f = this.getField("wells_datefield");
if (!f.value) f.value = util.printd ("m/d/yyyy", new Date());

Save your form and open it, and the date should pop right in there.

When making the document javascript, remember to not put it inside a function. One way of doing that is the following. In the Acrobat menu:

  1. Advanced->Document Processing->Document Javascripts...
  2. In the dialog that pops up, enter a Script Name (for instance "populate_date") and click Add...
  3. In the script dialog that pops up: REMOVE everything ("function populate_date(){}") and paste in the script above.
  4. Click ok, then Close.
  5. Save the form, close it and reopen it.
  6. There is no step 6! :>
Trophoplasm answered 16/7, 2010 at 8:49 Comment(2)
Paque, this works perfectly! Thank you so much. When the form is opened, the Document Javascript now inserts Today's date if the field is blank but does not overwrite the field if it already has a date. I have duplicated the code for other date fields: var g = ...; var h = ...; Thanks! (When I get enough points I'll vote this answer up, if I can.)Afebrile
Happy to help! (I think all you need to do is click the up arrow and check the box that pops up: Accept this as answer"... :> )Trophoplasm
S
1

Hope you don't mind if I add this edit of your code for British users (and newbies like me)

var f = this.getField("date");
if (!f.value) f.value = util.printd ("dd/mmm/yyyy", new Date());

This code fills the date field as follows...

03/Jan/2013
Sapanwood answered 3/1, 2013 at 22:37 Comment(0)
W
1

@wells-anderson

You just need to remove the if statement:

event.target.value = util.printd("m/d/yyyy", new Date());
Windbroken answered 2/10, 2015 at 22:23 Comment(0)
P
0

Unfortunately, when I add var f = this.getField("DatepPrinted"); if (!f.value) f.value = util.printd ("dd/mmmm/yyyy", new Date()); to my custom keystroke on a form it gives me back "0" instead of 02-april-2015..anyone know why?

Pshaw answered 2/4, 2015 at 13:41 Comment(1)
This looks like a different question posted as an answer. Please post this as a new question, if it is.Grosso

© 2022 - 2024 — McMap. All rights reserved.