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

Script to change date field format

Guest
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

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.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

7.2K

Translate

Translate

Report

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
Advocate ,
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Guest
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

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
Guest
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Advocate ,
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

Unless you can precisely control these pre-cooked functions, don't use them (they are undocumented; until about Acrobat 5, the according script was available in the clear; later on, they replaced it with a precompiled library, with the goal to speed up the application loading time). It is very likely that the argument of AFDate_Format() and AFDate_Keystroke() are incorrect.

Also, I wonder why you are using the RegEx matching for the field name match; wouldn't

if (fname == "date")

be easier to deal with?

Votes

Translate

Translate

Report

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
Guest
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

You are right. The right arguments are AFDate_FormatEx  and AFDate_KeystrokeEx.

I manually inserted AFDate_FormatEx("mm/dd/yyyy")  and AFDate_KeystrokeEx("mm/dd/yyyy") in the custom format options and it worked. Now, I need to figure out how to push this in there using JS.

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

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

I wonder if escaping quotes is not working.

I am looking at all fields which contain "date" in the name. There are fields like date of birth, date graduated, date first employed, etc.

Votes

Translate

Translate

Report

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
Advocate ,
May 10, 2016 May 10, 2016

Copy link to clipboard

Copied

OK, that explains the RegExp… you still could use something like

if (fname.toLowerCase().indexOf("date") >= 0)

It would be a long shot but try the direct syntax (not using the cTrigger / cScript)

You might also see whether it really inserts code by using something like app.beep() as script.

I may be wrong, but it is possible that if both, the Keystroke and the Format scripts are set, the date format dialog may appear instead of the custom script dialog.

Votes

Translate

Translate

Report

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
Guest
May 11, 2016 May 11, 2016

Copy link to clipboard

Copied

cTrigger / cScript work. For example:

f.setAction({cTrigger: "Format", cScript: "AFSpecial_Format(0);"});

f.setAction({cTrigger: "Keystroke", cScript: "AFSpecial_Keystroke(0);"});

These set the field to Zip format and it works well. Similarly the following set it as Phone number format.

f.setAction({cTrigger: "Format", cScript: "AFSpecial_Format(3);"});

f.setAction({cTrigger: "Keystroke", cScript: "AFSpecial_Keystroke(3);"});

It is the double quotes enclosing "mm/dd/yyyy" that seems to create the problem.

Votes

Translate

Translate

Report

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
Advocate ,
May 11, 2016 May 11, 2016

Copy link to clipboard

Copied

OK, as single quotes are also legal, try putting the format string between single quotes; in that case, there should be no need for backslashes.

Votes

Translate

Translate

Report

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 ,
Apr 26, 2019 Apr 26, 2019

Copy link to clipboard

Copied

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.

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!

Votes

Translate

Translate

Report

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 Beginner ,
Aug 18, 2023 Aug 18, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
New Here ,
Jun 27, 2024 Jun 27, 2024

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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