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

Convert date string to YYYY (Year) and copy value to another field

Explorer ,
Jun 25, 2018 Jun 25, 2018

Copy link to clipboard

Copied

Hello;

I'm looking for a script that will take a user's input of mm/dd/yy, convert the year to YYYY and copy this value to another field in the document's header. I think I almost had it at a certain point but I'm clearly lost now.

Here's what I've been able to put together. Is there any way that this can be done? I appreciate your help.

// convert date to yyyy and copy value to Specimen 1 Date Collected field in form header

var d = new Date (this.getField("Specimen 1 Date Collected").value);

var today = new Date();

if ((today.getFullYear() - d.getFullYear()) >= 100) {

     this.getField("Specimen 1 Date Collected Header").value = d + 100;

} else {

     this.getField("Specimen 1 Date Collected Header").value = d;

}

TOPICS
Acrobat SDK and JavaScript , Windows

Views

893

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

correct answers 1 Correct answer

Community Expert , Jun 25, 2018 Jun 25, 2018

Try this as the custom calculation script of the second text field:

// convert date to yyyy and copy value to Specimen 1 Date Collected field in form header

var v = this.getField("Specimen 1 Date Collected").valueAsString;

if (v=="") event.value = "";

else {

    var d = util.scand("mm/dd/yy", v);

    var today = new Date();

    if ((today.getFullYear() - d.getFullYear()) >= 100) {    

        event.value = d.getFullYear() + 100;

    } else {    

        event.value = d.getFullYear();

    }

}

Votes

Translate

Translate
Community Expert ,
Jun 25, 2018 Jun 25, 2018

Copy link to clipboard

Copied

Try this as the custom calculation script of the second text field:

// convert date to yyyy and copy value to Specimen 1 Date Collected field in form header

var v = this.getField("Specimen 1 Date Collected").valueAsString;

if (v=="") event.value = "";

else {

    var d = util.scand("mm/dd/yy", v);

    var today = new Date();

    if ((today.getFullYear() - d.getFullYear()) >= 100) {    

        event.value = d.getFullYear() + 100;

    } else {    

        event.value = d.getFullYear();

    }

}

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
Explorer ,
Jun 25, 2018 Jun 25, 2018

Copy link to clipboard

Copied

Thanks for the input!

Question;

My client has asked for the ability to simply enter in numeric digits (i.e. "010101") and have it convert automatically to mm/dd/yy (so not using the form field's Date format. I've gotten this to work on the first field as they want it to, however it looks like this might be working against your custom calculation script. I say this because I did as you suggested adding the script as a custom calculation on the second field but it didn't seem to be working as I tested. On a whim, I changed the form field format (for the first field) to a Date field with mm/dd/yy set under Date Options and voila! The header script worked perfectly.

How can I have the first field allow for custom formatting and the script for the second field still work? The client doesn't like that they have to either use the calendar pop-up or type in the slashes in order for the date to validate. They just want to quickly enter the numbers. Again, I've gotten that first field's automatic conversion of numeric digits to mm/dd/yy to work so far.

Thanks again.

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 ,
Jun 25, 2018 Jun 25, 2018

Copy link to clipboard

Copied

Change line #5 to:

var d = util.scand("mmddyy", v);

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
Explorer ,
Jun 25, 2018 Jun 25, 2018

Copy link to clipboard

Copied

LATEST

Oh, perfect!!!

Thank you so much!

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