Copy link to clipboard
Copied
So I have a form with multiple date fields. They all need to calculate first starting with an initial date field on the form
So, say it all starts with the field Date1, which is autopopulated when the form opens
Then
Date2 = Date1 + 9 months, last day of month (so if 9 months falls on 5/16/2018.... the date should read 5/31/2018 (last day of the month)
Date3 = Date1 + 1 year, last day of month
Date4 = Date]+ 21 months, last day of month
Date5 = Date1 + 2 years, last day of month
Can someone help with a script for this
and also tell me where the specific scripts are placed.
I sometimes get confused if Date2 script goes in Date2 or a field before.
Thanks for your help!
Instead of using the milliseconds, one can use the various "get" and "set" methods of the date object. We can start out with adding 9 months to the current date by converting the starting date string to the date object using the "util.scand" method. Next we can obtain the current month using the "getMoth()" method. We can now add 9 months using the current month value plus 9 in the "setMonth" method. Now we can use the undocumented trick of add 1 more month the to the date of the month and setting the date value to zero to get the last day of a month. We just now need to format the adjusted date object to the display format of the date using the "util.printd" method. To get to the 1 year value we can add 3 months to computed date object. To get to the 21 months we add another 9 months to the date object. And to get to 2 years we add another 3 months for a total of 24 months.
Assuming a date format of "d-mmm-yyyy" and field names of "NineMonths", "OneYear", "TwentyOneMonths" and "TwoYears" for the various result fields we can use the "On Blur" action for the inputted date field with the following script:
// for JavaScript date object, month end for a month is next month date 0;
// this can be used in the getMonth method to set the date to the end of the current month;
var cDate = event.value;
if(cDate != "")
{
var cFormat = "d-mmm-yyyy";
// get date object;
var oDate = util.scand(cFormat, cDate);
// 9 months;
// month end plus 9 months;
oDate.setMonth(oDate.getMonth() + 1 + 9, 0);
// month end plus 9 months;
this.getField("NineMonths").value = util.printd(cFormat, oDate);
// 1 year;
// month end plus 1 year;
oDate.setMonth(oDate.getMonth() + 1 + 3, 0)
this.getField("OneYear").value = util.printd(cFormat, oDate);
// 21 months;
//month end 21 months;
oDate.setMonth(oDate.getMonth() + 1 + 9, 0);
this.getField("TwentyOneMonths").value = util.printd(cFormat, oDate);
// 2 years;
// month end plus 2 years;
oDate.setMonth(oDate.getMonth() + 1 + 3, 0);
this.getField("TwoYears").value = util.printd(cFormat, oDate);
}
The above script will adjust the the end of the month for any date in February 2018 to February 29 2020 for the date plus 2 years.
Copy link to clipboard
Copied
So I have a form with multiple date fields. They all need to calculate first starting with an initial date field on the form
So, say it all starts with the field Date1, which is autopopulated when the form opens
Then
Date2 = Date1 + 9 months, last day of month (so if 9 months falls on 5/16/2018.... the date should read 5/31/2018 (last day of the month)
Date3 = Date1 + 1 year, last day of month
Date4 = Date]+ 21 months, last day of month
Date5 = Date1 + 2 years, last day of month
Can someone help with a script for this
and also tell me where the specific scripts are placed.
I sometimes get confused if Date2 script goes in Date2 or a field before.
Thanks for your help!
Instead of using the milliseconds, one can use the various "get" and "set" methods of the date object. We can start out with adding 9 months to the current date by converting the starting date string to the date object using the "util.scand" method. Next we can obtain the current month using the "getMoth()" method. We can now add 9 months using the current month value plus 9 in the "setMonth" method. Now we can use the undocumented trick of add 1 more month the to the date of the month and setting the date value to zero to get the last day of a month. We just now need to format the adjusted date object to the display format of the date using the "util.printd" method. To get to the 1 year value we can add 3 months to computed date object. To get to the 21 months we add another 9 months to the date object. And to get to 2 years we add another 3 months for a total of 24 months.
Assuming a date format of "d-mmm-yyyy" and field names of "NineMonths", "OneYear", "TwentyOneMonths" and "TwoYears" for the various result fields we can use the "On Blur" action for the inputted date field with the following script:
// for JavaScript date object, month end for a month is next month date 0;
// this can be used in the getMonth method to set the date to the end of the current month;
var cDate = event.value;
if(cDate != "")
{
var cFormat = "d-mmm-yyyy";
// get date object;
var oDate = util.scand(cFormat, cDate);
// 9 months;
// month end plus 9 months;
oDate.setMonth(oDate.getMonth() + 1 + 9, 0);
// month end plus 9 months;
this.getField("NineMonths").value = util.printd(cFormat, oDate);
// 1 year;
// month end plus 1 year;
oDate.setMonth(oDate.getMonth() + 1 + 3, 0)
this.getField("OneYear").value = util.printd(cFormat, oDate);
// 21 months;
//month end 21 months;
oDate.setMonth(oDate.getMonth() + 1 + 9, 0);
this.getField("TwentyOneMonths").value = util.printd(cFormat, oDate);
// 2 years;
// month end plus 2 years;
oDate.setMonth(oDate.getMonth() + 1 + 3, 0);
this.getField("TwoYears").value = util.printd(cFormat, oDate);
}
The above script will adjust the the end of the month for any date in February 2018 to February 29 2020 for the date plus 2 years.
Copy link to clipboard
Copied
When a script sets a value on a field, it is generally place in a calculation script on that field. But this changes depending on the situations.
Here is some article that explain how to script dates in Acrobat and PDF. They have example files, a couple of which show how to do parts of what you want.
https://acrobatusers.com/tutorials/date_time_part1
https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2
Your asking for some tricky calculations. For example, what's a month? month's vary in length, so what does it mean to advance a date one month? is it 30 days? 31 days? or the same date on the next month?
Copy link to clipboard
Copied
Instead of using the milliseconds, one can use the various "get" and "set" methods of the date object. We can start out with adding 9 months to the current date by converting the starting date string to the date object using the "util.scand" method. Next we can obtain the current month using the "getMoth()" method. We can now add 9 months using the current month value plus 9 in the "setMonth" method. Now we can use the undocumented trick of add 1 more month the to the date of the month and setting the date value to zero to get the last day of a month. We just now need to format the adjusted date object to the display format of the date using the "util.printd" method. To get to the 1 year value we can add 3 months to computed date object. To get to the 21 months we add another 9 months to the date object. And to get to 2 years we add another 3 months for a total of 24 months.
Assuming a date format of "d-mmm-yyyy" and field names of "NineMonths", "OneYear", "TwentyOneMonths" and "TwoYears" for the various result fields we can use the "On Blur" action for the inputted date field with the following script:
// for JavaScript date object, month end for a month is next month date 0;
// this can be used in the getMonth method to set the date to the end of the current month;
var cDate = event.value;
if(cDate != "")
{
var cFormat = "d-mmm-yyyy";
// get date object;
var oDate = util.scand(cFormat, cDate);
// 9 months;
// month end plus 9 months;
oDate.setMonth(oDate.getMonth() + 1 + 9, 0);
// month end plus 9 months;
this.getField("NineMonths").value = util.printd(cFormat, oDate);
// 1 year;
// month end plus 1 year;
oDate.setMonth(oDate.getMonth() + 1 + 3, 0)
this.getField("OneYear").value = util.printd(cFormat, oDate);
// 21 months;
//month end 21 months;
oDate.setMonth(oDate.getMonth() + 1 + 9, 0);
this.getField("TwentyOneMonths").value = util.printd(cFormat, oDate);
// 2 years;
// month end plus 2 years;
oDate.setMonth(oDate.getMonth() + 1 + 3, 0);
this.getField("TwoYears").value = util.printd(cFormat, oDate);
}
The above script will adjust the the end of the month for any date in February 2018 to February 29 2020 for the date plus 2 years.