Skip to main content
Inspiring
February 23, 2018
Question

How can I extract year (yyyy) from user entered date (mmddyyyy)?

  • February 23, 2018
  • 3 replies
  • 1155 views

Hello;

I'm working on an Acrobat DC PDF form and need to have one field be able to extract the year (format: yyyy) from another field where the user enters the entire date (format: mmddyyyy). This second field (where the user enters the date) should also automatically add in the slashes once the user leaves the field. How would I be able to do this?

Thanks for any help you can provide.

This topic has been closed for replies.

3 replies

Inspiring
March 16, 2018

Thanks for everyone's input. My client decided to go with something not as complicated but these answers definitely pointed me in the right direction.

Thanks!

Inspiring
February 23, 2018

Your use of the date format without item separators  could cause problems when users drop leading zeros. You should verify the length of the entered value and need to be careful to not drop the leading zeros when present.

I would convert the date string to the date object and then use the "getFullYear()" method to obtain the 4 digit year. I would also include the test to verify that the date is a valid date.

try67
Community Expert
Community Expert
February 23, 2018

Extracting the year is easier then adding the slashes. To do the former you just need to extract the last four characters of the field's value, using the substr method, for example. You can do it like this:

this.getField("FullDate").valueAsString.substr(-4);

The latter is more complicated. You would need to write custom format and validation scripts to do it.