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

Entering a date into a field and having the rest of the fieds populate

rowman25
Registered: May 1 2008
Posts: 19
Answered

I have a PDF form which users currently have to manually enter dates in each day of the week. I would like to make it so a user enters a date in the first box (date1) and the rest of the fields are populated with the next days so that

date1 = 02/22/2009 (entered manually)
date2 = date1 + 1 day (calculated)
date3 = date1 + 2 days (calculated)
date4 = date1 + 3 days (calculated)

and so on. Can anybody tell me how to accomplish this?

Thanks.

My Product Information:
Acrobat Standard 8.0, Windows
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi rowman25,

This can be done with JavaScript . It's a bit of ocde and field naming is important for it to work, so maybe a bit complex for a complete solution in a forum setting like this. This is hours of work for someone who really knows JS already.

Here is an article on working with dates and times in Acrobat using JavaScript that should get you started. Whne you have some code and a specific problem then this is a good place to get pinpoint type help.

http://www.acrobatusers.com/tutorials/2006/date_time_part2/

Hope this helps,

Dimitri
WindJack Solutions
www.pdfscripting.com
www.windjack.com
rowman25
Registered: May 1 2008
Posts: 19
Thank you for pointing me in the right direction. Here's what I was able to create:

2 text boxes in date format (date1, date2)

date2 has the following calculation script:

var firstdate = this.getField("date1").value;
if(firstdate.length)
{
var firstmilli = util.scand("mm/dd/yyyy",firstdate);

var oneDay = 24 * 60 * 60 * 1000;
var secondMillis = firstmilli.getTime() + oneDay;
var secondDate = new Date(secondMillis);
}

And has the follwing custom format script:

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

The problem I am having is that the calculation works as expected for the first date that I insert after pasting the script, but it doesn't update when I change the contents of date1 (or close and reopen the pdf). Any suggestions?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use a JavaScript for the on blur action for the 'date1' field, so the dates are only calculated when one passes through the 'date1' field instead of each time a field is updated.
var sNow = this.getField('date1').value;// see if there is a value for computationif(sNow == ''){// clear other date fields if field is emptythis.resetForm(['date2', 'date3', 'date4']);} else {// computation for none empty field// define some variablesvar mDay = 1000 * 60 * 60 * 24; //one day in millisecondsvar oDate; // date time object for datevar fDate; // value of date time objectvar fNext; // value for next date var oNext; // date time object for next datevar sNext; // formatted string for next date// convert date to date time objectoDate = util.scand('mm/dd/yyyy', sNow);fDate = oDate.valueOf(); // loop through days to addfor(i = 1; i <4; i++) {// add i days to datefNext = fDate + (mDay * i);// convert next day to date time objectoNext = new Date(fNext);// convert next day date time object to stringsNext = util.printd('mm/dd/yyyy', oNext);// assign next date string to date fieldthis.getField('date' + (i+1)).value = sNext;} // end day loop } // end not empty

George Kaiser

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
If you want to use a calculation for each field, use the following script and a field format of 'none':
event.value = ''; // clear result by default// see if we have datavar fFirstDate = this.getField("date1").value;if(fFirstDate.length) {var oneDay = 24 * 60 * 60 * 1000; // one day in millisecondsvar oFirstMilli = util.scand("mm/dd/yyyy",fFirstDate); // base date as  time objectvar fSecondMillis = fFirstMilli.valueOf() + oneDay; // add one day milliseconds to value of date time objectvar fSecondDate = new Date(fSecondMillis); // convert milliseconds to date time objectevent.value = util.printd("mm/dd/yyyy", fSecondDate) // format date time object}

George Kaiser

rowman25
Registered: May 1 2008
Posts: 19
Those are some good solutions. I ended up doing some more research and learned more aout creating and calling document level scripts. I used the following solution:

Document Script
------------------------------------------------------
function countDays(z){
var a = (1000*60*60*24) * z;
var b = 0;
var c = new Date();
var d = this.getField("date1").value;
if(d!=""){
var e = Date.parse(util.scand("mm/dd/yyyy", d));
c.setTime(e + a);
event.value = util.printd("mm/dd/yyyy",c);}
}
------------------------------------------------------

Field Calculation Script. Where '1' is how many days to add to the date...
------------------------------------------------------
countDays(1)
------------------------------------------------------

Thanks for your help.