Copy link to clipboard
Copied
Hi,
I need help creating a JavaScript in Acrobat to calculating 10 years from a Contract Date field. I'm having difficulty with getting this to come out correctly because i need to account for leap years. Any help would be greatly appreciated.
Thank you!
HI,
I am assuming you are using a date picker but it is no real problem if you are not, if you use the code below then the Date object will cope with all the leap years and such.
...// this is where I get the date from ( a standard date picker)
var myDate = this.getField("Date3_af_date").valueAsString;
// I make the string an actual date
var myDateValue = new Date ( myDate);
// get the years and add 10
myDateYears = myDateValue.getFullYear() + 10;
// get the months
myDateMonths = myDateValue.getMonth();
//
Copy link to clipboard
Copied
HI,
I am assuming you are using a date picker but it is no real problem if you are not, if you use the code below then the Date object will cope with all the leap years and such.
// this is where I get the date from ( a standard date picker)
var myDate = this.getField("Date3_af_date").valueAsString;
// I make the string an actual date
var myDateValue = new Date ( myDate);
// get the years and add 10
myDateYears = myDateValue.getFullYear() + 10;
// get the months
myDateMonths = myDateValue.getMonth();
// get the days
myDateDays = myDateValue.getDate();
// using the values above, create a new date, this should automatically deal with leap years and such
var myDate = new Date ( myDateYears, myDateMonths, myDateDays);
// put the returned value into a text field to see.
this.getField("Text4").value = myDate.toString();
Hope this helps
Malcolm
Copy link to clipboard
Copied
Thank so much Malcolm! How do I display this as mm/dd/yyyy in the script because if I try to use the Format tab it doesn't give me the correct date.
Copy link to clipboard
Copied
In the last line change this:
myDate.toString();
To:
util.printd("mm/dd/yyyy", myDate);
Also, if this is a calculation script, change the first part of that line to:
event.value = ...
Copy link to clipboard
Copied
Thank you so much try67. I really appreciate this.
Copy link to clipboard
Copied
Sorry but my end user now wants 1 month added to the above calculation.
So if my Contract Date is 07/13/2018, the new script above adds 10 years 07/13/2028, adding 1 month should return a result of 08/13/2028.
Copy link to clipboard
Copied
Change this line:
myDateMonths = myDateValue.getMonth();
To:
myDateMonths = myDateValue.getMonth()+1;
if (myDateMonths==12) {
myDateMonths = 0;
myDateYears++;
}
Copy link to clipboard
Copied
try67 You are the best! Thanks for making me look good. I feel like I should send you chocolate.
Copy link to clipboard
Copied
Now how do I get the default 01/00/0000 not to appear in the fields? Is there a validation script I can run?
Copy link to clipboard
Copied
Do you mean when the original field is empty?
Copy link to clipboard
Copied
Correct.
Copy link to clipboard
Copied
Since the code has been modified quite a bit I'm posting it in full again, in its latest form:
var myDate = this.getField("Date3_af_date").valueAsString;
if (myDate=="") event.value = "";
else {
var myDateValue = new Date(myDate);
var myDateYears = myDateValue.getFullYear() + 10;
var myDateMonths = myDateValue.getMonth()+1;
if (myDateMonths==12) {
myDateMonths = 0;
myDateYears++;
}
var myDateDays = myDateValue.getDate();
var myDate = new Date ( myDateYears, myDateMonths, myDateDays);
event.value = util.printd("mm/dd/yyyy", myDate);
}