Planet PDF Forum Planet PDF Forum
  New Posts New Posts RSS Feed - Add 5 years
  FAQ FAQ  Forum Search   Register Register  Login Login

Hi, welcome to the Foxit Planet PDF Forum. If you have PDF or Adobe Acrobat questions then the right place to ask them is here, in this forum.

Add 5 years

 Post Reply Post Reply Page  12>
Author
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Topic: Add 5 years
    Posted: 05 Feb 2016 at 5:44pm
I need to add five years to one of two date fields depending on whether a check box is checked or unchecked. It should default to "doi" + 5 years when the box is unchecked. I'm thinking it should be something like this, but it doesn't work. Can anyone assist?

// assign var b to check box
var b = this.getCheckBox("agg.revise");

if (b.target.value == "Yes"){
// get first closure date
var a = util.scand("mm/dd/yyyy",this.getField("firstclosure").value);
// get the year value from date object
var yr = a.getFullYear();
// get the month value from date object
var mo = a.getMonth();
// get the date value from the date object
var da = a.getDate();
// add 5 years to the year value
yr += 5; 
// create new date object from year+5, month, and date
var d2 = new Date(yr,mo,da);
// place result in current field
event.value = util.printd("mm/dd/yyyy", d2)}

else{
// get first closure date
var a = util.scand("mm/dd/yyyy",this.getField("doi").value);
// get the year value from date object
var yr = a.getFullYear();
// get the month value from date object
var mo = a.getMonth();
// get the date value from the date object
var da = a.getDate();
// add 5 years to the year value
yr += 5; 
// create new date object from year+5, month, and date
var d2 = new Date(yr,mo,da);
// place result in current field
event.value = util.printd("mm/dd/yyyy", d2)}
Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4109
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post Posted: 05 Feb 2016 at 6:40pm
Do you get any errors with your code either poping up or being displayed in the JavaScript console?

After creating a form with the fields needed for your calculation and entering your script into a field for the result, I find the following error message in the JavaScript console:

2:Field:CalculateTypeError: this.getCheckBox is not a function

Once that issue is cleared up, I then get an error:


3:Field:CalculateTypeError: b.target is undefined

"target" is only a property of the event.object and not the field object.

I do not know of any Acrobat JavaScript method named "getCheckBox". There is a "getFeild" method to get the object for a named field.

Also I am not sure I would force the month and day when adding just 5 years. Since the 5 years will include at least 1 leap year and it is possible the date you are adding 5 years to could be February 29 but in 5 years there will be February 29 in that year.

One can use the "getFullYear()" and "setFullYear" to add just 5 years to the date object and use the values for the month and date already present in the date object.



Edited by gkaiseril - 05 Feb 2016 at 7:03pm
Back to Top
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Posted: 05 Feb 2016 at 7:31pm
Thanks for the reply. No I don't get any errors. I wasn't sure if you needed to change "getField" when you are dealing with a check box. If I add a similar JavaScript (see below) to the check box instead of the field it works. The only problem is it doesn't produce a value in the "date.aggravationend" field until the check box is checked and/or unchecked. I really need it to default without the user checking the box. I'll look into the leap year problem seperately, thanks for the heads up on that.

var b = this.getField("date.aggravationend");

if (event.target.value == "Yes"){
// get first closure date
var a = util.scand("mm/dd/yyyy",this.getField("worker.doi").value);
// get the year value from date object
var yr = a.getFullYear();
// get the month value from date object
var mo = a.getMonth();
// get the date value from the date object
var da = a.getDate();
// add 5 years to the year value
yr += 5; 
// create new date object from year+5, month, and date
var d2 = new Date(yr,mo,da);
// place result in current field
b.value = util.printd("mm/dd/yyyy", d2)} 

else{
// get first closure date
var a = util.scand("mm/dd/yyyy",this.getField("date.firstclosure").value);
// get the year value from date object
var yr = a.getFullYear();
// get the month value from date object
var mo = a.getMonth();
// get the date value from the date object
var da = a.getDate();
// add 5 years to the year value
yr += 5; 
// create new date object from year+5, month, and date
var d2 = new Date(yr,mo,da);
// place result in current field
b.value = util.printd("mm/dd/yyyy", d2)}
Back to Top
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Posted: 05 Feb 2016 at 7:47pm
Ok you helped me fix it. This appears to work. I just changed getCheckBox to getField and b.target.value to b.value. Thanks.

var b = this.getField("agg.revise");

if (b.value == "Yes"){
// get first closure date
var a = util.scand("mm/dd/yyyy",this.getField("worker.doi").value);
// get the year value from date object
var yr = a.getFullYear();
// get the month value from date object
var mo = a.getMonth();
// get the date value from the date object
var da = a.getDate();
// add 5 years to the year value
yr += 5; 
// create new date object from year+5, month, and date
var d2 = new Date(yr,mo,da);
// place result in current field
event.value = util.printd("mm/dd/yyyy", d2)} 

else{
// get first closure date
var a = util.scand("mm/dd/yyyy",this.getField("date.firstclosure").value);
// get the year value from date object
var yr = a.getFullYear();
// get the month value from date object
var mo = a.getMonth();
// get the date value from the date object
var da = a.getDate();
// add 5 years to the year value
yr += 5; 
// create new date object from year+5, month, and date
var d2 = new Date(yr,mo,da);
// place result in current field
event.value = util.printd("mm/dd/yyyy", d2)}
Back to Top
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Posted: 05 Feb 2016 at 7:49pm
How would you suggest I change the script to deal with leap years?
Back to Top
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Posted: 05 Feb 2016 at 8:28pm
We tested it, and it appears to work even for leap years.
Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4109
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post Posted: 06 Feb 2016 at 11:19pm
I would just adjust the fullYear for the "a" object using the "setFullYear()" method

var a = util.scand("mm/dd/yyyy",this.getField("worker.doi").value);
// add 5 years to the date object;
a.setFullYear(a.getFullYear() + 5); 
// place result in current field
event.value = util.printd("mm/dd/yyyy", a);

and get rid of the unnecessary code.
Back to Top
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Posted: 10 Feb 2016 at 5:42pm
Thanks. That works too. Now I have a new problem related to leap years. I need the script to subtract 1 day if the value of a is 02/29.

Example:

02/29/2016 + 5 - 1 = 02/28/2021

Is there an easy way to make that adjustment to the current script?

Thanks for any assistance.
Back to Top
gkaiseril View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Location: USA
Points: 4109
Post Options Post Options   Quote gkaiseril Quote  Post ReplyReply Direct Link To This Post Posted: 10 Feb 2016 at 6:31pm
I would test the month and date for the date object and if equal to 229 then change the date object by deducting 1 day for the date value for the date object.
Back to Top
dan View Drop Down
New Member
New Member


Joined: 05 Feb 2016
Points: 8
Post Options Post Options   Quote dan Quote  Post ReplyReply Direct Link To This Post Posted: 11 Feb 2016 at 10:41pm
Ok, gave it a try but getting an error at line 9. Do you see whats causing the error?

// regarding check box
var a = this.getField("agg.revise");
// get date of injury
var d = util.scand("mm/dd/yyyy",this.getField("worker.doi").value);
// get date of first closure
var f = util.scand("mm/dd/yyyy",this.getField("date.firstclosure").value);
// analyze both dates for "Feb 29" (leap year); subtract one day when true
if (d.getMonth == 2) && (d.getDate == 29);{
d.setDate(28)
}
(f.getMonth == 2) && (f.getDate == 29);{
f.setDate(28)
}
// add 5 years to the date objects;
d.setFullYear(d.getFullYear() + 5)}
f.setFullYear(f.getFullYear() + 5)}
// place result in current field
// when agg box IS checked
if (a.value == "Yes") {
event.value = util.printd("mm/dd/yyyy", d)
}
// when agg box NOT checked
else{
event.value = util.printd("mm/dd/yyyy", f)
}
Back to Top
try67 View Drop Down
Senior Member
Senior Member


Joined: 15 Jul 2011
Points: 629
Post Options Post Options   Quote try67 Quote  Post ReplyReply Direct Link To This Post Posted: 12 Feb 2016 at 1:35pm
There are several errors in this part of the code:

if (d.getMonth == 2) && (d.getDate == 29);{
d.setDate(28)
}
(f.getMonth == 2) && (f.getDate == 29);{
f.setDate(28)
}

But I don't think this code is necessary at all. You can't create a Date object that points to an invalid date (like 29/09 on a non-leap year). You need to test it before converting the string to a Date object.

- Acrobat Expert - Contact me personally at try6767@gmail.com
Check out my custom-made scripts website: http://try67.blogspot.com
Back to Top
 Post Reply Post Reply Page  12>
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.10
Copyright ©2001-2017 Web Wiz Ltd.

This page was generated in 0.047 seconds.