Copy link to clipboard
Copied
I am using this script in the calculate field event: can anybody assist?
var dateFirst=StartDate1.formattedValue;
var dateSecond=Date1.formattedValue;
var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
Totaldays1=diffDays;
by all accounts this should work right?
You can use the following script to compute the difference in days. You may need to adjust the date string format and field names to match your form.
// start document level functions;
function GetField(oDoc, cField){
var oField = oDoc.getField(cField); // get field object for named field in specified PDF doc;
if(oField == null){
// trap and report error;
app.alert("Field " + cField + " not found.\nCheck for existance of field.", 1, 0, "Field Access Error");
}
return oField; // return field object;
}
func
...Copy link to clipboard
Copied
What is formattedValue? I have never seen this.
Copy link to clipboard
Copied
Also, that's not how you access the value of a field, assuming this is an Acrobat form.
Copy link to clipboard
Copied
Ok, How do i access field values in acrobat DC? and would having to cast the field value to toString() work?
startDate1.toString();
Copy link to clipboard
Copied
What are the field names?
Copy link to clipboard
Copied
If the field is called "StartDate1" then you access its value like this:
this.getField("StartDate1").valueAsString
Note that JavaScript is case-sensitive, so if the field is actually named "startDate1" the code above won't work.
Copy link to clipboard
Copied
And to apply the result to the calculating field change the last line to:
event.value = diffDays;
Copy link to clipboard
Copied
I have adjusted the code but still Not working:
var dateFirst=this.getField(StartDate1);
var dateSecond=this.getField(endDate1);
var timeDiff=Math.abs(dateSecond.getTime()-dateFirst.getTime());
var diffDays=Math.ceil(timeDiff /(1000 * 3600 * 24));
this.Totaldays1=diffDays;
StartDate1,endDate1 and Totaldays1 are the fields in the form....perhaps i am failing to assign the value i suppose
the rest are just varibale to hold dats for the calculation.:
Copy link to clipboard
Copied
You did not adjust it correctly. Study the example I gave carefully.
Copy link to clipboard
Copied
Press ctrl+j and look for errors in the debugger.
Copy link to clipboard
Copied
full code runs with no errors perhaps my assignment and accessing the fields might be the problem.
var dateFirst=this.getField("StartDate1").valueAsString;
var dateSecond=this.getField("endDate1").valueAsString;
var timeDiff=Math.abs(dateSecond.getTime()-dateFirst.getTime());
var diffDays=Math.ceil(timeDiff /(1000 * 3600 * 24));
this.Totaldays1=diffDays;
Copy link to clipboard
Copied
Please read reply #6 again, as it tells you exactly what is wrong with your last line.
Copy link to clipboard
Copied
var dateFirst=this.getField("StartDate1").valueAsString;
var dateSecond=this.getField("endDate1").valueAsString;
var timeDiff=Math.abs(dateSecond.getTime()-dateFirst.getTime());
var diffDays=Math.ceil(timeDiff /(1000 * 3600 * 24));
event.value = diffDays;
//please note i am doing this in properties calculated field tab in Totaldays1's field under custom calculation script maybe the approach is wrong
Copy link to clipboard
Copied
Read reply #12.
Copy link to clipboard
Copied
Beside the last line, which you didn't change, there's another issue. You're treating the dateFirst and dateSecond variables as if they're Date objects. They're not. They just strings. You need to first convert them to Date objects (using util.scand) and then you'll be able to use the getTime method on them.
Copy link to clipboard
Copied
You code if using FormCalc and that script only works in forms created using LiveCycle.
Copy link to clipboard
Copied
You can use the following script to compute the difference in days. You may need to adjust the date string format and field names to match your form.
// start document level functions;
function GetField(oDoc, cField){
var oField = oDoc.getField(cField); // get field object for named field in specified PDF doc;
if(oField == null){
// trap and report error;
app.alert("Field " + cField + " not found.\nCheck for existance of field.", 1, 0, "Field Access Error");
}
return oField; // return field object;
}
function Scand(cFormat, cDateTime){
var oDate = util.scand(cFormat, cDateTime); // convert date string to date object using format;
if(oDate == null){
// trap and report conversion error;
app.alert("Error converting " + cDateTime + " using format " + cFormat, 1, 0, "Date/Time Conversion Error");
}
return oDate;
}
function Date2Days(cFormat, cDateTime){
var nDays = null;
var oDate = Scand(cFormat, cDateTime); // convert to date object;
if(oDate != null){
oDate.setHours(0, 0, 0, 0); // set to midnight;
nDays = Math.floor((oDate.getTime() / (1000 * 60 * 60 * 24))); // convert to whole days;
}
return nDays;
}
// end document level functions;
var nDiffDays = "";
var cDateFormat = "d-mmm-yyyy"; // change as needed to match date field format;
var oStart = GetField(this, "StartDate"); // field for start date;
var oEnd = GetField(this, "endDate"); // field for end date;
if(oStart != null && oStart.value != "" && oEnd != null && oEnd.value != ""){
// compute difference in days;
nDiffDays = Date2Days(cDateFormat, oEnd.value) - Date2Days(cDateFormat, oStart.value);
}
event.value = nDiffDays; // set field value;
The functions are used to minimize the amount of repeated code. The functions also include error reporting and trapping to help you in your debugging if needed. FormCalc includes some similar functions as built-in functions, but with Acrobat JavaScript one has to provide the code to do the job of the built-in functions.