Skip to main content
Known Participant
June 9, 2016
Answered

Show date results into three different fields?

  • June 9, 2016
  • 1 reply
  • 3138 views

Is it possible to have the date entered to show in three different fields? For example, if I enter 3/13/2016 I would like to show it like this, Third month, thirteenth day, Year of 2016. In three separate fields. Can someone help with a script to do it? Thanks guys.

This topic has been closed for replies.
Correct answer Karl Heinz Kremer

Mr Kremer,

I believe you taught me something today regarding scripting. I have been trying to learn more and more everyday. The thing is about reading books is understanding and relating what I'm reading to what I'm trying to do. I find it so daunting and confusing. I read all the links and references I receive, but at times it makes me overwhelmed. But, I stick with it with the hopes of something getting into the brain pan. And, at times it does. This is what got thru.

This is the script I came up with, with your help and guidance. To my own surprise, I got it. This works: Did I get it correct?

var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var theDay = ""; 

var theYear = ""; 

 

// get the information from the "theDate" field 

var theDate = this.getField("theDate").valueAsString; 

 

if (theDate != "") { 

    // convert the date string to a JavaScript Date object 

    var d = util.scand("mmmm,dd,yyyy", theDate); 

    if (d != null) { 

        theMonth = monthNames[d.getMonth()]; 

        theDay = d.getDate(); 

        theYear = d.getFullYear(); 

    } 

event.value = theMonth; 

this.getField("Day").value = theDay; 

this.getField("Year").value = theYear; 

this.getField("Month").value = theMonth; 

Thank you for the invaluable time you spend helping people and myself. I really appreciate it.


I completely understand, programming is hard, and it requires a different way of thinking.

You are almost there. It may work, but there are two issues I see that might bite you later.

The first one is that you should "declare" the "theMonth" variable (which was in my original script, but you probably removed it when you added the array of month names. You are already declaring (and initializing) the variables "theDay" and "theYear", so just add this line to these two:

var theMonth = "";

And, then at the end you can remove one line - this is the calculation script for the "theMonth" field, which means that we do not have to set the value of the field via 'this.getField("theMonth").value' - because it is a calculation script, you set the value of the calculated field using the 'event.value' property. So, remove this line from the end of your script:

this.getField("Month").value = theMonth;

That should do it.

1 reply

Inspiring
June 9, 2016

Do you want that same string to appear in 3 different fields or do you have a field for the month, date and year you want populated.

Do you have any scripting or coding experience?

Acrobat JavaScript and JavaScript do not have any function for ordinal numbering and does not have a number to words function, so this will require some additional coding to get the ordinal month number from the date string as well as converting numbers to words.

pdfUser1Author
Known Participant
June 9, 2016

I have three text fields. One called Month, one called Day, on called Year. The date field I'd like to enter the date formatted m,dd,yyyy. It doesn't need to be converted into the wording because that's already set in the text when I created the doc in InDesign. I would just like to split up the date and have the numbers go into the text fields.

The way the fields appear in the doc is like this:

Day_______, Month_______, Year_______. Then I have txtFields in place where the blanks are.

As far as coding. I have a script to automatically populate the formatted date field. It's a real short script I place in the doc level. My coding experience is very novice at best. Like most forum participants here. But, I can take instructions real well and I'm very good at learning from examples. This bit of scripting shouldn't be all that complicated and is why I'm asking for a bit of help.

Karl Heinz  Kremer
Community Expert
Community Expert
June 10, 2016

Here is some background about how the JavaScript engine in Acrobat works:

There are two parts to each JavaScript implementation: The core language, which is the same for all JavaScript environments, and the application specific extensions (which as the name implies are in this case specific to Acrobat). The good news is that the core JavaScript language has a "Date" object, which allows you to convert from and to dates, and extract data from such a date object (like e.g. the day, month and year that make up the Date object). See here for more information about what you can do with a Date object:

Date - JavaScript | MDN

To convert from a string to a Date object, you can either use the functionality built into the Date object, or a method that Acrobat provides (which in general works better):

Acrobat DC SDK Documentation - util.scand

Once you combine all that togther, you very likely come up with something like this:

var theMonth = "";

var theDay = "";

var theYear = "";

// get the information from the "theDate" field

var theDate = this.getField("theDate").valueAsString;

if (theDate != "") {

    // convert the date string to a JavaScript Date object

    var d = util.scand("m,dd,yyyy", theDate);

    if (d != null) {

        theMonth = d.getMonth();

        theDay = d.getDate();

        theYear = d.getFullYear();

    }

}

event.value = theMonth;

this.getField("Day").value = theDay;

this.getField("Year").value = theYear;

This assumes that you have four fields, one is called "theDate", which contains your date in the somehow strange format m,dd,yyyy - bt you can change the format by changing the first parameter of util.scand(), and three fields called Month, Day and Year. The script would be used as the custom calculation script for the "Month" field.