These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Assistance with date calculation for newbie, please?

MaggieS
Registered: Apr 21 2010
Posts: 38

I created an Acrobat form (Acrobat 9.3.2 Pro). In a Date field, the user enters the date they complete the form. Once the user has entered the Date field, I'd like another field on the 2nd page of the form to automatically complete with a date 3 business days from the date entered by the user.

I really know nothing about Javascript, and although this seems like it ought to be a relatively easy calculation to do, I have absolutely no clue how to go about doing it. I've done some reading from internet posts, and the only thing it's done is get me more confused.

I doubt I'll ever need to learn much Javascript programming, so I was hoping that one of you kind souls could assist in the code that would make this work.

The form was created in a PDF, not from LiveCycle.

Thanks!!!!!

Maggie

My Product Information:
Acrobat Pro 9.3.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Here's how the code should work:
- Read the first field's value.
- Parse it to a Date object using util.scand()
- Convert it to milliseconds using the getTime() method. (the last two steps can be done using Date.parse() as well)
- Now comes the tricky part: You can add three days to the date by adding 259200000 milliseconds to it (the equivalent of 3 days), but then you still need to check whether the result is a business day or not.
Ignoring holidays, I would use the getDay() method, and if it returns 5 or 6 add another day or two to the result.
- Parse the result back to a string using util.printd() and apply it to event.value .

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

MaggieS
Registered: Apr 21 2010
Posts: 38
Thanks, try67. I think I understand the concept, but putting it into the form is beyond my capabilities. Appreciate your help, anyway.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can use the custom calculation script of the computed date field with either of the following code:
// using parts of a date// adjust sName for field and sDateFmt for format of date// input field namevar sName = "Date0";// number of days to addvar nAdd = 3;// date format for input and displayed datevar sDateFmt = 'mm/dd/yyyy';// do not change below this line// get inputted date stringvar sDate = this.getField(sName).value;// convert date string to date objectvar oDate = util.scand(sDateFmt, sDate); // get parts of the datevar nFullYear = oDate.getFullYear(); // full yearvar nMonth = oDate.getMonth(); // zero based monthvar nDate = oDate.getDate(); // date of month// add nAdd days to datenNewDate = nAdd + nDate; // convert to new date objectvar oNewDate = new Date(nFullYear, nMonth, nNewDate);// make date stringvar sNewDate = util.printd(sDateFmt, oNewDate);// set field valueevent.value = sNewDate;

or
// using date object and milliseconds// adjust sName for field and sDateFmt for format of date// input field namevar sName = "Date0";// number of days to addvar nAdd = 3;// date format for input and displayed datevar sDateFmt = 'mm/dd/yyyy'; // do not change below this line// get inputted date stringvar sDate = this.getField(sName).value;// convert date string to date objectvar oDate = util.scand(sDateFmt, sDate); // define some date constantsvar nSec = 1000; // milliseconds in one secondvar nMin = 60 * nSec; // seconds in one minutevar nHr = 60 * nMin; // minutes in one hourvar nDay = 24 * nHr; // hours in one day // get number of milliseconds from date objectvar nDate = oDate.getTime();// add nAdd days days to datenNewDate = nAdd * nDay + nDate; // convert to new date objectvar oNewDate = new Date(nNewDate);// make date stringvar sNewDate = util.printd(sDateFmt, oNewDate); // set field valueevent.value = sNewDate;

George Kaiser

MaggieS
Registered: Apr 21 2010
Posts: 38
gkaiseril---THANK YOU!!! I used the first block of code, and it works beautifully to calculate 3 days out from the date entered. Is there a way to adjust the code so that it calculates 3 Business Days out (i.e., does not include Saturday and Sunday?). Thank you, thank you again for responding so quickly.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can use the 'getDay()' method of the date object to obtain the zero based day of the week starting on Sunday.

With that value you can easily compute the adjustment needed to get to the next business day, but this will not adjust for holidays.

George Kaiser