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

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

Explorer ,
Jun 12, 2023 Jun 12, 2023

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 + ".";

TOPICS
JavaScript , PDF , PDF forms
1.2K
Translate
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Jun 12, 2023 Jun 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");

View solution in original post

Translate
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 15, 2023 Jun 15, 2023

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;

View solution in original post

Translate
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 12, 2023 Jun 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");

Translate
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 15, 2023 Jun 15, 2023

Thank you!  That resolved my issue!

Translate
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 15, 2023 Jun 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?

Translate
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 15, 2023 Jun 15, 2023

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

Translate
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 15, 2023 Jun 15, 2023
LATEST

I'm creating holiday closing notices and on occassion, we're closed more than one day.  I have three fields in my pdf; two date fields ("day1" and "day2") where the form user can select the dates and a third field (days) automatically combines the value of those two fields.  For example, the notice reads "We will be closed Saturday, June 18th, and Monday, June 19th in observance of....".

 

Fields "day1" and "day2" are viewable only for the form user to select the date and they do not print.  Field "days" is read only and does print.

 

Thank you for your help!

Translate
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 15, 2023 Jun 15, 2023

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;

Translate
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