Copy link to clipboard
Copied
How do I write a script to calculate a future date based on the product of 2 separate fields?
For example, Field1 represents a starting date (1/1/2023), Field2 represents (76days) from the starting date.
Copy link to clipboard
Copied
Is Field2 always 76 days from the starting date?
This question has been asked many times:
Copy link to clipboard
Copied
No, the 76 is not a fixed number.
That number will vary depending on user entries.
Copy link to clipboard
Copied
Ok, so Field1 is the starting date in "mm/dd/yyyy" format.
How is the number of days determined? Is it entered in Field2?
What is the field that recieves the final calculated date?
Details are important.
Copy link to clipboard
Copied
Let's call the receiving field (Field3)
Copy link to clipboard
Copied
Clearly I'm a novice at this.
Thanks for following up.
Copy link to clipboard
Copied
Here's the basic code outline, this is for a calculation script for Field3
1. Get date string string from Field1 and convert into Date Object
2. Get number of days to add to date from Field2
3. If both the starting date and #of days are valid
then Add # of days to current date
4. Format resulting date and set as calculation value
You can read about dates here:
https://www.pdfscripting.com/public/Date-and-Time-Handling.cfm
https://acrobatusers.com/tutorials/date_time_part2/
Also see the threads I posted earlier that show how to add days to a date.
When you come up with a script. Post it and we'll help you debug it.
Copy link to clipboard
Copied
So here's what I have so far:
var date= util.scand("mm/dd/yyyy",this.getField("startdate").value;
date.setDate(date.getDate()+365*3)
event.value=util.printd("mm/dd/yyyy",date)
As you can see, in this case, I manually entered 365*3 repesenting an expiration date of 3 yrs from the date entered in field1(startdate). What I would like is to be able to enter a random number into a separate field2 (duration), and then have the results displayed in field3(expiration). Thanks again for your help.
Copy link to clipboard
Copied
The script looks great!! Except it won't work because of a syntax error on line#1. But excellent try.
And it needs a little protection from empty fields and bad dates.
Here's an update with Field2 adding the # of days
var nDays = Number(this.getField("Field2").valueAsString);
var strStart = this.getField("startdate").valueAsString;
if(nDays && Number.isInteger(nDays) && strStart)
{
var dtStart = util.scand("mm/dd/yyyy",strStart);
if(dtStart){
dtStart.setDate(dtStart.getDate()+ nDays);
event.value=util.printd("mm/dd/yyyy",dtStart);
}
else
event.value = "";
}
else
event.value = "";
Copy link to clipboard
Copied
Thanks again.
I will give it a shot.
Since I'll be mostly doing calculations in "years" rather than "days", I'll just add (*365) to the formula and see if it works.
Copy link to clipboard
Copied
If you work with years, then you can use setFullYear and getFullYear().
Copy link to clipboard
Copied
Thanks for your input.
Copy link to clipboard
Copied
I was able to piece it together and make it work. Thanks for all of your assistance and input.