Skip to main content
Inspiring
June 25, 2018
Answered

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

  • June 25, 2018
  • 1 reply
  • 1408 views

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;

}

This topic has been closed for replies.
Correct answer try67

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();

    }

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 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();

    }

}

Inspiring
June 25, 2018

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.

try67
Community Expert
Community Expert
June 25, 2018

Change line #5 to:

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