How do I convert a two digit year to a four digit year in a date field

I am using Acrobat XI. I have a form with a lot of date fields. A business requirement is that the form must use a four digit date. I used the Format tab of the Field Properties to set the date format to a 4 digit year. This causes the form to throw an error if someone enters a date with a two digit year, forcing the user to re-enter the date using the correct four-digit year. The result is correct, but the process is not user-friendly.

Is there a way to have the field automatically convert an entry that uses a 2 digit year (yy) to a four digit year (yyyy), so the user does not have re-enter the date?


S E


2 Answers

Voted Best Answer

It looks like my formatting got messed up. To keep the blank entry, use the following code:

// get the current field value
var v = event.value;

if (v != "") {
    // try to convert the value to a date using the mm/dd/yyyy format
    var d = util.scand("mm/dd/yyyy", v);
    if (d == null) {
        // try to convert using the mm/dd/yy format
        d = util.scand("mm/dd/yy", v);
    }
    if (d != null) {
        event.value = util.printd("mm/dd/yyyy", d);
        event.rc = true;
    } else {
        app.alert("Please enter a correct date");
        event.rc = false;
    }
}


By Karl Heinz Kremer   

Create a custom validation script for this field and use this script:

// get the current value of the filed var v = event.value;

// try to convert the value to a date using the mm/dd/yyyy format
var d = util.scand("mm/dd/yyyy", v);
if (d == null) {
    // try to convert using the mm/dd/yy format
    d = util.scand("mm/dd/yy", v);
}
if (d != null) {
    event.value = util.printd("mm/dd/yyyy", d);
    event.rc = true;
}
else {
    app.alert("Please enter a correct date");
    event.rc = false;
}

Then remove any formatting that you've applied to this field. You should now be able to enter a date in mm/dd/yy or mm/dd/yyyy format, and have it converted to mm/dd/yyyy.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   


Please specify a reason: