Skip to main content
JR Boulay
Community Expert
Community Expert
November 2, 2019
Answered

Assign the Date format to a field via JavaScript

  • November 2, 2019
  • 10 replies
  • 13626 views

Hi

 

Is it possible to assign the Acrobat's buit-in Date format to a field via JavaScript?
I tried several things but without success:

this.getField("myField").setAction("Format", Date);

this.getField("myField").setAction("Format", "date(fr){DD MMMM YYYY}");

Etc.

 

Thank you

This topic has been closed for replies.
Correct answer try67

It's possible, if you use one of the options in the list. If you use a format that doesn't appear in the list it will be defined as "Custom". You can do it using this code:

 

var f = this.getField("myField");
var cFormat = "mm/dd/yyyy";
f.setAction("Format", "AFDate_FormatEx(\""+cFormat+"\")");
f.setAction("Keystroke", "AFDate_KeystrokeEx(\""+cFormat+"\")");

10 replies

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 5, 2019

Many hours of reading to come!

😉

Thank you.

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 5, 2019

"You have to look at the source code"

Where can I find it?

Acrobate du PDF, InDesigner et Photoshopographe
ls_rbls
Community Expert
Community Expert
November 5, 2019

Hi, 

 

Check here in this guide:

http://www.acrotex.net/blog/wp-content/uploads/2011/07/pdfblog_22.pdf 

 

Pages 9 through 13 --->>>>    15.2. Format Category: Number

 

this pdf guide has examples with source code and fields that you can practice on

ls_rbls
Community Expert
Community Expert
November 5, 2019

And in the Acrobat Forms API Reference

 

There is a link in this page to download:

http://www.lystech.com/webhelp/Content/format-pdf-text-box/advanced.htm 

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 4, 2019

I tried this, but it just does nothing…

I cannot figure how to assign the Zip code format.

 

f.setAction("Format", "AFZipEntryRegExp");
f.setAction("Keystroke", "AFZipCommitRegExp");

 

Please, please, please.

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
November 4, 2019

You have to look at the source code of each function/property to find out what it does.

I looked it up in my own code (from this paid-for tool I've created: http://try67.blogspot.com/2012/06/acrobat-apply-format-to-multiple-fields.html) and the methods you need to use are called AFSpecial_Format and AFSpecial_Keystroke.

try67
Community Expert
Community Expert
November 3, 2019

There are all undocumented methods, built-in to the application. I don't recall at the moment the names of the methods for Zip Code and the rest, but it should be possible.

Thom Parker
Community Expert
Community Expert
November 3, 2019

All of the custom formatting functions are defined in folder level scripts that ship with Acrobat. In later versions these script are encoded into an intermediate binary format, but if you have an older version of acrobat sitting around, just look in the app JavaScript folder. There's lots of interesting undocumented code. 

 

You can also find these functions by running this script in the console.

 

for(nm in this){if(/^AF/.test(nm))console.println(nm);}

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 4, 2019

Thank you Thom

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 3, 2019

try67 you are a JavaScript JEDI !

 

It works fine.

I have questions more:

- what about for a "Zip code" format field? (I need it too)

- what about for another built-in format? (just because I'm curious)

- where can I find documentation about "AFDate", "FormatEx" and "KeystrokeEx"?

(There is nothing related in the "Developing Acrobat® Applications Using JavaScript™" nor in the "JavaScript™ for Acrobat® API Reference".)

 

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
November 2, 2019

It's possible, if you use one of the options in the list. If you use a format that doesn't appear in the list it will be defined as "Custom". You can do it using this code:

 

var f = this.getField("myField");
var cFormat = "mm/dd/yyyy";
f.setAction("Format", "AFDate_FormatEx(\""+cFormat+"\")");
f.setAction("Keystroke", "AFDate_KeystrokeEx(\""+cFormat+"\")");

vgvallee
Participant
September 24, 2021

Very helpful post. Here are some more informations in case someone else has the requirements as mine.

 

Using AFDate_FormatEx and AFDate_KeystrokeEx worked perfectly in cases where a specific format is required. However, I had the case where many formats were to be accepted and the error message provided by the "Keystroke" event had to be in the language of the file used (and not the program language). I wanted to roll my own functions to setAction but was running into the problem where the calendar drop down would not be shown on the field or would not always work. Turns out that the code to be supplied to setAction with "Format" has to be a function starting with "AFDate_FormatEx". So this code does not show the drop down:

f.setAction("Format", "some_function(\""+cFormat+"\")");

whereas this one does:

f.setAction("Format", "AFDate_FormatEx_some_function(\""+cFormat+"\")");

Double quotes should be used around the argument as the use of the single quotes like the call below seemed to be a problem:

f.setAction("Format", "AFDate_FormatEx_some_function('"+cFormat+"')");

The calendar drop down is shown the first time but it will not work drop anymore after one use. Same if the function call does not have a string parameter:

f.setAction("Format", "AFDate_FormatEx_some_function()");

The "Keystroke" event can have any name and no or any number of parameters.

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 2, 2019

"What do you mean by "Acrobat's buit-in Date format"?"

 

I mean not a custom JavaScript format. See attached screenshots.

 

 

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
November 2, 2019

"Is the Acrobat built-in format considered the same as the current system date?"

 

I would prefer to be able to choose the date format more directly, but otherwise it could be appropriate

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
November 2, 2019

What do you mean by "Acrobat's buit-in Date format"?

ls_rbls
Community Expert
Community Expert
November 2, 2019

Is the Acrobat built-in format considered the same as the current system date?