Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Calculating future dates from data contained in multiple fields

Community Beginner ,
Sep 05, 2023 Sep 05, 2023

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.

TOPICS
JavaScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023

Is Field2 always 76 days from the starting date?

 

This question has been asked many times:

https://community.adobe.com/t5/forums/searchpage/tab/message?q=add%20days%20to%20date&noSynonym=fals...

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2023 Sep 05, 2023

No, the 76 is not a fixed number.

That number will vary depending on user entries.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023

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. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2023 Sep 05, 2023

Let's call the receiving field (Field3)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2023 Sep 05, 2023

Clearly I'm a novice at this.

Thanks for following up.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023

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. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 06, 2023 Sep 06, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 06, 2023 Sep 06, 2023

 

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 = "";

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 2023

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. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 07, 2023 Sep 07, 2023

If you work with years, then you can use setFullYear and getFullYear().

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 07, 2023 Sep 07, 2023

Thanks for your input.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 13, 2023 Sep 13, 2023
LATEST

I was able to piece it together and make it work.  Thanks for all of your assistance and input.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines