Print Page | Close Window

Add 5 years

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: JavaScript
Forum Description: Discussions relating to the use of JavaScript within PDF.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=1936
Printed Date: 17 Jun 2019 at 5:56pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: Add 5 years
Posted By: dan
Subject: Add 5 years
Date 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)}



Replies:
Posted By: gkaiseril
Date 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.



Posted By: dan
Date 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)}


Posted By: dan
Date 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)}


Posted By: dan
Date Posted: 05 Feb 2016 at 7:49pm
How would you suggest I change the script to deal with leap years?


Posted By: dan
Date Posted: 05 Feb 2016 at 8:28pm
We tested it, and it appears to work even for leap years.


Posted By: gkaiseril
Date 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.


Posted By: dan
Date 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.


Posted By: gkaiseril
Date 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.


Posted By: dan
Date 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)
}


Posted By: try67
Date 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


Posted By: gkaiseril
Date Posted: 12 Feb 2016 at 5:23pm
Why are you adjusting the date back one day?

You are cutting the 5 years one days short.

Anyone with a birthday of February 29 celebrates on March 1.

The year is determined by the Earth's orbit around the Sun and to get to the same position in the orbit 5 years later will be April 1st with our calendar system.




Posted By: dan
Date Posted: 12 Feb 2016 at 8:56pm
Because the Great State of Oregon legislatively told us we had to.Unhappy

uploads/2848/Industry_102.pdf - uploads/2848/Industry_102.pdf



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net