Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

How do I add a current date to date field?

Enthusiast ,
Nov 30, 2024 Nov 30, 2024

I have a Date Field and I'm trying to preload it with the current date; however, it's not working. This is the script I have:

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

 

Date4 is the name of the Date Field.

2Charlie_0-1732984521305.png

I don't see the current load when in Preview mode.

 

 

 

 

TOPICS
How to , JavaScript , PDF forms
509
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2024 Nov 30, 2024

For validate script you need to change field value for the script to trigger, so either use under calculate tab or as one of the action (Mouse UP) it all depends on how you wish to trigger script and if you wish script to update date or remain fixed.

Also, you can shorten script like this: event.value = util.printd("m/d/yyyy", new Date());

If you use as action script, change event.value to event.target.value

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Nov 30, 2024 Nov 30, 2024

Thank you so much for the help. I want to automatically show the current date when the PDF is open but yet allows the user to pick a different date if he/she wants to. I put the script on the page properties as shown below it's still not showing the current date when the form is opened in Acrobat.

2Charlie_0-1732985661307.png

 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 30, 2024 Nov 30, 2024
LATEST

Move your code to a doc-level script, via Tools - JavaScript - Document JavaScripts. Create a new item there called "scripts", delete the function that is created by default and paste your code, and you're done.

However, be advised this will overwrite any input the user entered into the field the next time they open the file.

If you don't want to do that then add this before the first second line:

if (f.valueAsString=="")

But then it would not update the field, even if the user didn't enter anything into it... Basically it will only update it if the field was empty when the file was opened.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines