Skip to main content
DawnP2023
Participating Frequently
November 2, 2020
Question

Date field format issues

  • November 2, 2020
  • 3 replies
  • 813 views

I have a date field that once filled in triggers calculations in other field (set due dates two weeks out).  This field is formatted as mmmm d, yyyy but when someone types in Oct 31 it shows October 31, 2020 but the calculation in the other field does not calculate.  The other field is a read only calculate.

I am using Adobe Acrobat Pro DC.

This topic has been closed for replies.

3 replies

DawnP2023
DawnP2023Author
Participating Frequently
November 2, 2020

I have a validation script as follows.  How do I check for other than for "" or null?:

if(event.value != "") { // process non-empty string
     var oMyDate = util.scand("mmmm d, yyyy", event.value); // convert to date object
     if(oMyDate == null) {
            app.alert("Invalid date entered! Please enter in the format: mmmm d, yyyy. For example, January 1, 2020.", 0, 1), "Date Validation"; // check validity
            event.value = util.printd("mmmm d, yyyy", oMyDate); // strict format
}
}

Thom Parker
Community Expert
Community Expert
November 2, 2020

This validation has a couple of issues.  Also, use the "util.scand" function in your calculation. 

 

First, I use a regular expression to check for an empty string.  Use this in your first line of code

 

if(event.value && !/^\s*$/.test(event.value))

 

Next, the "util.printd" line is inside the "if" block that only activates when "oMyDate" is null. Obviously this won't work. Move to the next line outside the "if" block.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
DawnP2023
DawnP2023Author
Participating Frequently
November 2, 2020

Do you mean this?

if(event.value && !/^\s*$/.test(event.value)) { // process non-empty string
     var oMyDate = util.scand("mmmm d, yyyy", event.value); // convert to date object
     if(oMyDate == null) {
          app.alert("Invalid date entered! Please enter in the format: mmmm d, yyyy. For example, January 1, 2020.", 0, 1), "Date Validation"; // check validity
     }
     event.value = util.scand("mmmm d, yyyy", oMyDate); // strict format
}

Thom Parker
Community Expert
Community Expert
November 2, 2020

When someone enters a "Oct 31" into the field, then that is the value of the field.  The formatting only changes how the field value is displayed. So it looks like a complete date, but it isn't. The field needs to validate that is has a complete date value. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
November 2, 2020

Although true, the scand method is quite lenient and will assume you're referring to the current year if you don't specify it, so it should (in theory) work. For example, this code:

util.scand("mmmm d, yyyy", "October 31")

Will return the following Date object:

Sat Oct 31 2020 00:00:00 GMT+0100 (Romance Standard Time)

Even specifying just "Oct 31" will work...

DawnP2023
DawnP2023Author
Participating Frequently
November 2, 2020

It does default to October 31, 2020 correctly but my calculated field doesn't calculate.  Here is teh calculation:

var irDate = this.getField("InvoiceReceivedDate");
var d = new Date(irDate.value);
var weekDay = d.getDay();

 

if (irDate.length > 0){
     if (weekDay == 6){
          d.setDate(d.getDate() + 13);
     }else if (weekDay == 0){
          d.setDate(d.getDate() + 12);
     }else {
          d.setDate(d.getDate() + 14);
     }
     this.getField("DueDateFacilities").hidden = false;
     event.value = util.printd("dddd mmmm d, yyyy", d);
}else{
     this.getField("DueDateFacilities").hidden = true;
}

Bernd Alheit
Community Expert
Community Expert
November 2, 2020

What script does you use for the calculation?

DawnP2023
DawnP2023Author
Participating Frequently
November 2, 2020

Here is the calculation:

var irDate = this.getField("InvoiceReceivedDate");
var d = new Date(irDate.value);
var weekDay = d.getDay();

 

if (irDate.length > 0){
     if (weekDay == 6){
          d.setDate(d.getDate() + 13);
     }else if (weekDay == 0){
          d.setDate(d.getDate() + 12);
     }else {
          d.setDate(d.getDate() + 14);
     }
     this.getField("DueDateFacilities").hidden = false;
     event.value = util.printd("dddd mmmm d, yyyy", d);
}else{
     this.getField("DueDateFacilities").hidden = true;
}