Copy link to clipboard
Copied
How do I compute an "EndDate" by adding the number of months in field "AgreementMonths" to a date in "StartDate".
The fields are StartDate, AgreementMonths and EndDate.
Dates are formatted October 19, 2019 (mmmm d, yyyy)
StartDate + AgreementMonths = EndDate
Copy link to clipboard
Copied
Date calculations are done using JavaScript custom calculations or actions. Date calculations or formatting cannot be done in the Simplified field notation or any of the "field value is the __________ of the following fields. All string date values need to be converted to thed JavaScript Date object. Adobe has provided some documentation about Using the Date Object and performing Date arithmetic.
It is easiest to convert date strngs to a Date object by using the util.scand method. This method will report an invalid date string by returning a null value. One can use JavaScript to manipulate date objects and extract data from the Date object. One can get the fullYear, month, day, hour, minute, second, and milliseconds as well as set them.
Taking all the above, one can use for dates with the format of "d-mmm-yyyy".
// custom calculation script for EndDate;
// get start date string;
var cStartDate = this.getField("StartDate").valueAsString;
// convert date string to date object;
var oStartDate = util.scand("d-mmm-yyyy", cStartDate);
// set date time to midnight by setting seconds and milliseconds to 0;
oStartDate.setSeconds(0, 0);
if(oStartDate == null) {
app.alert("Date Converstion Error start date " + cStartDate, 1, 0);
}
// get length of agreement in months;
var nAgreementMonths = this.getField("AgreementMonths").value;
// set end date to start date plus lenght of agreements in months;
var oEndDate = oStartDate;
oEndDate.setMonth(oEndDate.getMonth() + nAgreementMonths);
// set field value to formatted comuted end date;
event.value = util.printd("d-mmm-yyyy", oEndDate);
Note JavaScript does not use the last day of the month but the number of days in the start month.
Copy link to clipboard
Copied
Date calculations are done using JavaScript custom calculations or actions. Date calculations or formatting cannot be done in the Simplified field notation or any of the "field value is the __________ of the following fields. All string date values need to be converted to thed JavaScript Date object. Adobe has provided some documentation about Using the Date Object and performing Date arithmetic.
It is easiest to convert date strngs to a Date object by using the util.scand method. This method will report an invalid date string by returning a null value. One can use JavaScript to manipulate date objects and extract data from the Date object. One can get the fullYear, month, day, hour, minute, second, and milliseconds as well as set them.
Taking all the above, one can use for dates with the format of "d-mmm-yyyy".
// custom calculation script for EndDate;
// get start date string;
var cStartDate = this.getField("StartDate").valueAsString;
// convert date string to date object;
var oStartDate = util.scand("d-mmm-yyyy", cStartDate);
// set date time to midnight by setting seconds and milliseconds to 0;
oStartDate.setSeconds(0, 0);
if(oStartDate == null) {
app.alert("Date Converstion Error start date " + cStartDate, 1, 0);
}
// get length of agreement in months;
var nAgreementMonths = this.getField("AgreementMonths").value;
// set end date to start date plus lenght of agreements in months;
var oEndDate = oStartDate;
oEndDate.setMonth(oEndDate.getMonth() + nAgreementMonths);
// set field value to formatted comuted end date;
event.value = util.printd("d-mmm-yyyy", oEndDate);
Note JavaScript does not use the last day of the month but the number of days in the start month.
Copy link to clipboard
Copied
Works perfectly.
I haven't used JavaScript but will definitely begin learning it.
Thanks