Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
What script does you use for the calculation?
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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...
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
I believe the problem is with this line:
var d = new Date(irDate.value);
You can only do that if the value has a very specific format. Use util.scand, instead.
Also, this is incorrect:
if (irDate.length > 0){
It needs to be:
if (irDate.valueAsString.length > 0){
Copy link to clipboard
Copied
I updated the var d line to "var d = util.scand("mmmm d, yyyy", irDate);" and the if statement to
"if (irDate.valueAsString.length > 0){" and now the dates are incorrect and DueDateFacilities is hidden. Here is the whole thing:
var irDate = this.getField("InvoiceReceivedDate");
var d = util.scand("mmmm d, yyyy", irDate);
var weekDay = d.getDay();
if (irDate.valueAsString.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.scand("dddd mmmm d, yyyy", d);
}else{
this.getField("DueDateFacilities").hidden = true;
}
Copy link to clipboard
Copied
Following line is incorrect :
event.value = util.scand("dddd mmmm d, yyyy", d);
Copy link to clipboard
Copied
Nope, still wrong. This line:
var d = util.scand("mmmm d, yyyy", irDate);
Needs to be:
var d = util.scand("mmmm d, yyyy", irDate.valueAsString);
You have to keep track of the kind of object your variables are pointing to. Is it a field? A string? A number? An array? etc.
Copy link to clipboard
Copied
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
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
Not exactly, That line needs to be in an else.
if(oMyDate == null){}
else
event.value = util.scand(...);
IOnly one or the other can be done.