Skip to main content
May 10, 2016 7:51 AM UTC
Question

Script to change date field format

I have several forms each eith 20 to 30 fields which have no formatting and are just text fields.  I want to programmatically change all fields with name containing "date" to mm/dd/yyyyy format. Is this possible?

 

I am new to Adobe scripting but I know some javascript. I could not find a method to change the field format from Adobe's JS guide and some quick internet searches did not yield anything that I could work on.

12 replies

PDF Automation Station
Community Expert
Community Expert
Nov 5, 2025 4:26 PM UTC
Colin Flashman
Community Expert
Community Expert
Apr 26, 2019 7:14 AM UTC

Late to the party here but I think I have an answer and it lies in a linked reference:

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

It goes into great detail how to apply formatting to a date field.

Translate
If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Participating Frequently
Aug 18, 2023 8:24 AM UTC

The acrotex site is down. Does anyone have some other source?

Translate
Participant
Jun 27, 2024 6:34 PM UTC

I guess this one is the same: https://www.math.uakron.edu/~dpstory/PDFBlog/pdfblog_22.pdf

I have attached it to this post.

Regards,

    --Julio

Translate
Legend
May 10, 2016 8:11 AM UTC

You can not directly change the field format using JavaScript. But you can add a format script using the setAction() method. This method allows to add a JavaScript to a list of events of the Field processing event sequence.

What you'd have to know, however, is where the Date Object comes from (be it a variable, or be it just "now"). The example below assumes that the current date should be used.

In order to set the format of the date, we use the util.printd() method.

Example; you would run this code from the Console while the document is open.

this.getField("date").setAction("Format", "event.value = util.printd(\"mm/dd/yyyy\", new Date() ) ;") ;

And that should get you on the way.

Hope this can help.

Translate
May 10, 2016 10:13 AM UTC

Max, thanks for the response. I made some progress but I am stuck with this:

var mymatch = new RegExp("date");

for (var i = 0; i < this.numFields; i++) {

    var fname = this.getNthFieldName(i);

    if (mymatch.test(fname)) {

    var f = this.getField(fname)  

    f.setAction({cTrigger: "Format", cScript: "AFDate_Format(\"mm/dd/yyyy\")"});

    f.setAction({cTrigger: "Keystroke", cScript: "AFDate_Keystroke(\"mm/dd/yyyy\")"});

    }

}

It sets the fields as date fields but the format is "m/d". Not "mm/dd/yyyy".  Any ideas?

Translate
May 10, 2016 11:12 AM UTC

I just discovered that the format is not enforced and I can type in any values in the box. The field property shows format as m/d but there is no restriction.

Translate