Skip to main content
Participant
May 1, 2017
Answered

Converting date field into three separtate, formatted fields

  • May 1, 2017
  • 2 replies
  • 555 views

Hello,

I am trying to find a script that will convert a date field (XX/XX/XX) into three separate fields to populate the following text:

In WITNESS WHEREOF, the respective parties thereto have executed this instrument this _____day of

_____________, 20________.

I am a novice at best with JavaScript and I have not been able to locate anything online to assist me with this problem.

Can anyone out there help??

This topic has been closed for replies.
Correct answer gkaiseril

Assuming the input date field is named "Date" and the 3 output fields are "Day", "Month", and "Year", the following validation script for the "Date" field will populate the 3 output fields.

// document level function;
function ordinal(i) {
/*
purpose: convert inter to ordinal number;
input: any integer;
return: intieger and ordinal suffix;
*/
    var cOrdinal = "";
    var sufixes = new Array("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th");
    switch (i % 100) {
    case 11:
    case 12:
    case 13:
         cOrdinal = i + "th";
    break;
    default:
        cOrdinal = i + sufixes[i % 10];
     break;
    } // end switch;
    return cOrdinal;
} // end of ordinal function;
// end document level function


// custom validation script;
// can include above document level function;
var cDate = "";
var cMonth = "";
var cYear = "";
if(event.value != "") {
cFormat = "mm/dd/yy"; // must be the same format as the field being validated;
var oDate = util.scand(cFormat, event.value); // convert to date objec;
// validate input date;
if(oDate == null)
{
  app.alert("Input date is not a valid date", 1, 0, "Entry Error");
} else {
  // date as ordinal number;
  var cDate = ordinal(oDate.getDate());
  // spelled out month;
  var cMonth = util.printd("mmmm", oDate);
  // two digit year;
  var cYear = util.printd("yy", oDate);
} // end date validation;
} // end target value not null;
// set field values;
this.getField("Day").value = cDate;
this.getField("Month").value = cMonth;
this.getField("Year").value = cYear;
// end validation script;

The above code will also validate the inputted date and clear the output fields if the input field is blank.

2 replies

Inspiring
May 1, 2017

We need to know what positions are the month, day, and year since there are some countries that use the "mm/dd/yy" format and others use the "dd/mm/yy" format.

Also do you want the month spelled out and do you want to use the ordinal number of the day?

ErichVAuthor
Participant
May 1, 2017

Great questions, I should have been more specific.  The date field is in the mm/dd/yy format.

The month must be spelled out.

And if possible, the date should appropriate suffix (e.g. 1st, 15th)

gkaiserilCorrect answer
Inspiring
May 1, 2017

Assuming the input date field is named "Date" and the 3 output fields are "Day", "Month", and "Year", the following validation script for the "Date" field will populate the 3 output fields.

// document level function;
function ordinal(i) {
/*
purpose: convert inter to ordinal number;
input: any integer;
return: intieger and ordinal suffix;
*/
    var cOrdinal = "";
    var sufixes = new Array("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th");
    switch (i % 100) {
    case 11:
    case 12:
    case 13:
         cOrdinal = i + "th";
    break;
    default:
        cOrdinal = i + sufixes[i % 10];
     break;
    } // end switch;
    return cOrdinal;
} // end of ordinal function;
// end document level function


// custom validation script;
// can include above document level function;
var cDate = "";
var cMonth = "";
var cYear = "";
if(event.value != "") {
cFormat = "mm/dd/yy"; // must be the same format as the field being validated;
var oDate = util.scand(cFormat, event.value); // convert to date objec;
// validate input date;
if(oDate == null)
{
  app.alert("Input date is not a valid date", 1, 0, "Entry Error");
} else {
  // date as ordinal number;
  var cDate = ordinal(oDate.getDate());
  // spelled out month;
  var cMonth = util.printd("mmmm", oDate);
  // two digit year;
  var cYear = util.printd("yy", oDate);
} // end date validation;
} // end target value not null;
// set field values;
this.getField("Day").value = cDate;
this.getField("Month").value = cMonth;
this.getField("Year").value = cYear;
// end validation script;

The above code will also validate the inputted date and clear the output fields if the input field is blank.

try67
Community Expert
Community Expert
May 1, 2017

As the custom validation code of the text field you can enter something like this:

if (event.value) {
    this.getField("Day").value = event.value.split("/")[0];

    this.getField("Month").value = event.value.split("/")[1];

    this.getField("Year").value = event.value.split("/")[2];
} else this.resetForm(["Day", "Month", "Year"]);

You'll probably need to adjust the field-names in the code to match those in your file...