Skip to main content
gwendolyny47862191
Participant
May 18, 2018
Question

Calculating future date in a form

  • May 18, 2018
  • 2 replies
  • 3287 views

I have a form with two manual dates entered.  Later in the form, I need to return a date 30 days later than the earliest of the two dates.  How do I program the form to do this?

This topic has been closed for replies.

2 replies

gwendolyny47862191
Participant
May 20, 2018

I was able to find someone who could help me, and this is the custom calculation script that was created for anyone else who may have the same issue (it works)!

  // Custom calculate script for "31ST DAY" field 

    (function () { 

     

        // Get date from field 

        var v = getField("ORIGINAL DISPENSE DATE").valueAsString; 

        // Get date from field 

        var x = getField("DATE DISPENSED").valueAsString;   

     

        // Convert string to date 

        var d = util.scand("mm/dd/yy", v); 

        // Convert string to date 

        var dx = util.scand("mm/dd/yy", x);    

                    

        // Populate this field with the result 

        if (v) { 

             // Add 30 days 

            d.setDate(d.getDate() + 30);

 

            event.value = util.printd("mm/dd/yy", d); 

        } else { 

            if (x ) {

                // Add 30 days 

            dx.setDate(dx.getDate() + 30);

            event.value =  util.printd("mm/dd/yy", dx);

            } else {

            event.value =  "";

        }

            }

})();

HarlemTax
Participant
May 18, 2020

Thx, I used this procedure as best I could to calculate the 56th  day for purpooses of the PPP Loan Forgiveness Application.

 

But ran into a small problem, that I can deal with: when the beginning date field is empty, then my result field is the last value, and I'd like for it to be a blank field, below is the script that I entered:

 

var v = getField("COVERED_PERIOD").valueAsString;
// Convert string to date

var d = util.scand("mm/dd/yy", v);
// Add 30 days

d.setDate(d.getDate() + 56);

event.value = util.printd("mm/dd/yyyy", d);

 

Any suggestions?

 

Thx in advance

Thom Parker
Community Expert
Community Expert
May 19, 2020

You need to test the input to the process 

 

var v = getField("COVERED_PERIOD").valueAsString;

if(/\d{1,2}\/\d{1,2}\/\d{4}/test(v))

{

    .. rest of script

}

else
    event.value = "";

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
May 18, 2018

You will need to use custom JavaScript. The question each of finding earliest date and add days or other period have been asked and entered many many times. You will need to convert the date strings values of the 2 fields to a JavaScript date object, you can use the "util.scand()" method., You can get the earliest date by comparing the results of the "getTime()" method of the date object. You can use the "getDate()" and ".setdate()" to add 30 days to the selected date object. You can then use Acrobat's "util.printd()" method to format the result.

gwendolyny47862191
Participant
May 18, 2018

Thank you, this sounds very technical and I am a amateur.  Can you please walk me through it?