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

AcroForms and the JavaScript date object help needed!

RKF
Registered: Jul 13 2009
Posts: 20
Answered

I need to create a form that allows users to enter departure and arrival dates and times. The form needs to compute the total time spent traveling minus meal/break times. Because the time periods can start and end on different dates, I need to factor in the date in the calculations but I am not sure how. I have also been asked to round the solution to the nearest quarter hours.

Example: Departure1 = 7/17/09 10:45 am, Arrival1 = 7/17/09 1:30 pm, Meals/Breaks1 = 30 minutes, total travel time = 2:30 hours

I use the Standard version of Acrobat 9, but I am not sure how to use AcroForms and the JavaScript date object.

I have been reading everything I can locate on time calculations and I have looked at Thom Parker's tutorial series on Working with date and time in Acrobat JavaScript but I am still having problems.

Honestly I think I am more confused.

Please Help!

My Product Information:
Acrobat Standard 9.1.1, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The general principal is to convert the date character string or date and time string to a JavaScript date object and then get the number of milliseconds from the Epoch date as milliseconds. With this value one can more easily perform various time date calculations. One can use the 'util.scand()' method to convert date time character strings to the JavaScirpt date object.

Entering both the time and date as one field in the correct format can be difficult. So assuming you have separate fields for the dates and times of departure and arrival makes life easier for the end user. So assuming you have the following fields in the following formats:

Departure Date
field: 'departure.date'
format 'm/DD/YY'

Departure Time
field: 'departure.time'
format 'h:MM tt'

Arrival Date
field: 'arrival.date'
format 'm/DD/YY'

Departure Time
field: 'arrival.time'
format 'h:MM tt'

First, one needs to convert the date string and the time string to minutes, the smallest time element needed for this task. For this we need to know the format of the date and time values and the date and time values. Since this task will be done more than once, we can create a custom function that will make a single string of the combined date and time values with a known format.

From this string value and format, one can compute the number of milliseconds format the Epoch date for this date and time. Again since this function will occur more than once and is useful for other date time calculations one can create a custom function to perform this calculation and return the results in minutes.
function DateTime2Num(cFormat, cDate, cTime) {// using cDate field and cTime field compute minutesvar sDateTime = ''; // string for date & timevar fDateTime = ''; // time value in minutes - null for no datavar sDate = this.getField(cDate).value;var sTime = this.getField(cTime).value;// process if cDate and cTime are not emptyif(sDate != '' & sTime != '') {// build string from date and time valuessDateTime = sDate + ' ' + sTime;// convert date and time string to minutesfDateTime = Time2Num(cFormat, sDateTime) ;}return fDateTime;} // end DatTime2aNum function Time2Num(cFormat, cTime) {// use cFormat to convert cTime to minutes// create date object from cFormat and cTimevar oDate = util.scand(cFormat, cTime);// convert date object to numeric valuevar fDate = oDate.valueOf();// convert milliseconds to minutesvar fMin = fDate / (1000 * 60);// return rounded minutes to calling itemreturn Math.round(fMin);} // end Time2Num

With the number of minutes from the Epoch date for the departure and arrival, one can compute the total travel time in minutes by subtracting the departure minutes from the arrival minutes.

// compute departure as minutes from Epoch date using the departure date and time fieldsvar fDeparture = DateTime2Num('m/DD./YY h:MM tt', 'departure.date', 'departure.time'); // compute arrival as minutes from Epoch date using the arrival date and time fieldsvar fArrival = DateTime2Num('m/DD./YY h:MM tt', 'arrival.date', 'arrival.time'); // compute total travel time in minutesvar fTTime = 0; // total travel time// compute if departure and arrival values are not nullif(fDeparture != '' & fArrival != '') {fTTime = fArrival - fDeparture;app.alert('The travel time in minutes is: ' + fTTime, 0, 3);}

You now need only subtract the meal time and convert the resulting minutes to hours and minutes and format the display string.

George Kaiser

RKF
Registered: Jul 13 2009
Posts: 20
I have been working with my problem and came up with the Below script and it works - yea! But, I still need to adjust the final time with a break time subtracted and the final value rounded to the nearest 15 minute - ugh!

Now I have four more fields: BreakStart, BreakEnd, BreakTotal, and CTTHours (this is the field that needs to be rounded to the nearest 15 minute). All fields are HH:MM.

Can I do this??
Regina


// Time Values
var cStartTime = this.getField("Time1").value;
var cEndTime = this.getField("Time2").value;

// Only process if field contains a value
if((cStartTime != "") && (cEndTime != ""))
{
// Convert to Hours Decimal value
var aStartTime = cStartTime.split(":");
var nStartTime = Number(aStartTime[0]) + Number(aStartTime[1])/60;
var aEndTime = cEndTime.split(":");
var nEndTime = Number(aEndTime[0]) + Number(aEndTime[1])/60;

// Find Difference
var nTimeDiff = nEndTime - nStartTime;
var nHours = Math.floor(nTimeDiff );
var nMinutes = Math.floor((nTimeDiff - nHours)*60 + 0.5);


// If used in a calculation, may need to be changed to "event.value ="
this.getField("TravelTimeTotal").value = util.printf("%02d:%02d",nHours,nMinutes);

}
else
this.getField("TravelTimeTotal").value = "";
RKF
Registered: Jul 13 2009
Posts: 20
I have figured out how to subtract the break time; however, I am still having problems rounding the final figure to the nearest 15 minute.

Any Suggestions?
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Look this thread.
The one there got a solution for rounding to quarter hours.

http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=20348

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

RKF
Registered: Jul 13 2009
Posts: 20
I have attempted to use the information posted but I am unable to use document level functions or I should say I don't know how to use document level functions. :(
RKF
Registered: Jul 13 2009
Posts: 20
I created another text box for the rounded figure and used the following script:

// convert Times to minutes
var cStartTime = this.getField("Final").value / (1000 * 60);

// round Times to nearest 15 min
cStartTime = Round2Nearest(cStartTime, 15);

// convert Times to hours
cStartTime = cStartTime / 60;

// round Times to 1/100 th of an hour
cStartTime = Round2Nearest(cStartTime, 0.01);

// end document level functions

However, I keep getting "Round2Nearest is not defined
5:Field:Calculate" error.......
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
That means you have not added the document level script to the PDF.
function Round2Nearest(fValue, fMin) {return (Math.round(fValue / fMin)) * fMin;} // end round 2 nearest function

George Kaiser

RKF
Registered: Jul 13 2009
Posts: 20
How do I add that when using 9 standard?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Here is a real simple way to get the closest 15 minutes increment. The calculation starts with your minutes calc.

// Your minitues calculationvar nMinutes = Math.floor((nTimeDiff  - nHours)*60 + 0.5); // Now round it to the closest 15 minute IncrementnMinutes = 15*Math.round(nMinutes/15);

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

RKF
Registered: Jul 13 2009
Posts: 20
THANK YOU THANK YOU THANK YOU!!! My form looks good and all the auto-calculations work! Thank you to everyone who helped me!