Skip to main content
buddycafe
Known Participant
May 9, 2016
Answered

Spit MM,DD,YYYY in two fields?

  • May 9, 2016
  • 1 reply
  • 1338 views

Good afternoon,

Is there a way to split MM,DD,YYYY in two fields where the month is spelled out (can be any month of the year)? A previous thread showed me how to slice the characters in two (for example) fields but i don't know how it can be used since value.length varies depending on number of characters in a month. Is there light at the end for this scenario?

Example: March 14, 2016

Field1: March 14

Field2: 2016

Where the number of characters in month varies.

Thanks,

Edgar A.

This topic has been closed for replies.
Correct answer maxwyss

The Date object is not described in the Acrobat JavaScript documentation (because it is part of the JavaScript Core). However, you will find good descriptions online (or, if you are serious, get Flanagan's book JavaScript, the ultimate Guide, published by O'Reilly).

Acrobat JavaScript has not only the util.prind() method, but also the util.scand(), which takes a string (which should represent a date), and creates a valid Date object from that string's value and the format string.

So, if you have a field with a value 01/14/2016, the format string would be mm/dd/yyyy, and the code to create a Date object

var myDate = util.scand("mm/dd/yyyy", this.getField("myField").valueAsString) ;

And based on that, you can get quite a bit of input working (note that this is a single line of code).

1 reply

Legend
May 9, 2016

That could be done with a little bit of (Acrobat) JavaScript:

Let's assume that the field for month and date is named dat.month, and the field for the year dat.year.

You somehow provide the date to be used, and pass it as a Date Object "theDay"

In the general calculation script, you would then add the following two lines of code:

this.getField("dat.month").value = util.printd("mmmm d", theDay) ;

this.getField("dat.year").value = util.printd("yyyy", theDay) ;

And that would do it.

As said, you would have to create the proper date object.

Hope this can help.

buddycafe
buddycafeAuthor
Known Participant
May 10, 2016

maxwyss, will this work if the date can be any day of the year and not necessarily today's date? I'm currently researching more about "date ojbject" but i'm starting to get the gist. The PDF form is generated by the Interface. The user will select (for example) a product and that product was purchased on a particular date.

One more thing. The interface will import the date already formatted... Such as January 14, 2016. But I also have the ability to import it as 01/14/2016 if it helps.

maxwyssCorrect answer
Legend
May 11, 2016

The Date object is not described in the Acrobat JavaScript documentation (because it is part of the JavaScript Core). However, you will find good descriptions online (or, if you are serious, get Flanagan's book JavaScript, the ultimate Guide, published by O'Reilly).

Acrobat JavaScript has not only the util.prind() method, but also the util.scand(), which takes a string (which should represent a date), and creates a valid Date object from that string's value and the format string.

So, if you have a field with a value 01/14/2016, the format string would be mm/dd/yyyy, and the code to create a Date object

var myDate = util.scand("mm/dd/yyyy", this.getField("myField").valueAsString) ;

And based on that, you can get quite a bit of input working (note that this is a single line of code).