Skip to main content
sandys1348
Inspiring
June 12, 2023
Answered

Combine date fields to auto fill a different field with custom formatting

  • June 12, 2023
  • 1 reply
  • 1134 views

I'm trying to auto fill a custom formatted field ("days") by combining two date fields ("day 1" and "day 2") with "and" in the middle.  For example, if the user selects 6/2/2023 in the "day 1" field and 6/3/2023 in the "day 2" field, I want the "days" field to auto fill with "Friday, June 2nd, and Saturday, June 3rd".

 

I've added Custom Format Script and Custom Calculation Script to the "days" field and my dates are combined but not reformatted.  I can't get the custom formatting to add the st, nd, rd or th to the day and drop the year.

 

MY CUSTOM FORMAT SCRIPT:

 
var date1 = this.getField("day1").valueAsString;
var date2 = this.getField("day2").valueAsString;
 
if (date1.value) {
var oDate = util.scand("mmm/d/yyyy", date1.value);
if (oDate!=null) {
var sDD = formatOrdinalDay(oDate);
      date1.value = (util.printd("dddd, mmmm ", oDate) + sDD);
     }
}
 
if (date2.value) {
var oDate = util.scand("mmm/d/yyyy", date2.value);
if (oDate!=null) {
var sDD = formatOrdinalDay(oDate);
date2.value = (util.printd("dddd, mmmm ", oDate) + sDD);
     }
}
 
function formatOrdinalDay(d) {
if (d.getDate()==11 || d.getDate()==12 || d.getDate()==13) return d.getDate() + "th";
else if ((d.getDate()%10==1)) return d.getDate() + "st";
else if ((d.getDate()%10==2)) return d.getDate() + "nd";
else if ((d.getDate()%10==3)) return d.getDate() + "rd";
else return d.getDate() + "th";
}
 
MY CUSTOM CALCULATION SCRIPT:

var date1 = this.getField("day1").value;
var date2 = this.getField("day2").value;


event.value = date1 + " and " + date2 + ".";

This topic has been closed for replies.
Correct answer sandys1348

I resolved my issue with the "days" field not updating the date values if they are changed in the "day1" or "day2" fields by using the following:

 

FIELDS "day1" and "day2"

FORMAT - Date, Custom: "dddd, mmmm d"

VALIDATE - Run Custom Validation Script:

 

if (event.value) {
var oDate = util.scand("mmm/d/yyyy", event.value);
if (oDate!=null) {
var sDD = formatOrdinalDay(oDate);
event.value = (util.printd("dddd, mmmm ", oDate) + sDD);
}
}
 
function formatOrdinalDay(d) {
if (d.getDate()==11 || d.getDate()==12 || d.getDate()==13) return d.getDate() + "th";
else if ((d.getDate()%10==1)) return d.getDate() + "st";
else if ((d.getDate()%10==2)) return d.getDate() + "nd";
else if ((d.getDate()%10==3)) return d.getDate() + "rd";
else return d.getDate() + "th";
}
 
FIELD "days":
FORMAT:  none
CALCULATE - Custom Calculation Script:
 

var date1 = this.getField("day1").value;
var date2 = this.getField("day2").value;


event.value = date1 + " and " + date2;

1 reply

try67
Community Expert
June 12, 2023

The date1 and date2 variables are already a string. As such, you don't need to use the "value" property on them to access their values. So either remove all the instances in the code where you do that, or simply change the first two lines to:

var date1 = this.getField("day1");
var date2 = this.getField("day2");

sandys1348
Inspiring
June 15, 2023

If I change the value of field(s) "day1" or "day2", the formatting in my "days" field does not work.  The first day remains formatted as "day of week, month date(with st nd rd or th)" but he second day is "month day, year".  It doesn't format correctly unless I go into the custom calculation of the "days" field and save it again.  How do I make it recaculate if the "day1" or "day2" field value gets changed?

try67
Community Expert
June 15, 2023

Sorry, I don't follow. What are you trying to achieve with this format script?