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

Calculating future date in a form

Community Beginner ,
May 18, 2018 May 18, 2018

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?

TOPICS
PDF forms
3.4K
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
LEGEND ,
May 18, 2018 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.

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 ,
May 18, 2018 May 18, 2018

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

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 ,
May 20, 2018 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 =  "";

        }

            }

})();

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
New Here ,
May 18, 2020 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

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 ,
May 19, 2020 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 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
New Here ,
Jan 09, 2023 Jan 09, 2023

How would I have it calculate a date for 2 years in the future?  Example, contract signed date plus 2 years to show the from date (date signed) to the "to" date (date 2 years later when it expires).

Judy Beck
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 ,
Jan 09, 2023 Jan 09, 2023
LATEST

Let's say you have the fields named "Date1" and "Date2" and you wish to set "Date2" to be two years from whatever is entered in "Date1".

Use this as 'Validate' script of "Date1" field:

var date2 = this.getField("Date2");
if(event.value == "")
date2.value = "";
else{
var date1 = util.scand("mm/dd/yyyy", event.value);
date1.setFullYear(date1.getFullYear()+2);
date2.value = util.printd("mm/dd/yyyy", date1);}
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