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

converting time entries to whole numbers and decimals

sjsmith
Registered: Sep 2 2008
Posts: 28
Answered

I have a timesheet that converts time entries to hours with ".00." It also rounds off to the quarter hour. Right now, it subtracts the "in" time from the "out" time and then converts it to hours and rounds them off. After working on it for some time, I need change the script to round off the time to the quarter hour BEFORE it does the arithmatic. Does anyone have a suggestion as to how I could do this with some reworking instead of starting over?

Here's the script:

// create JavaScript date time object for start time
var oStart = util.scand("yyyy/mm/dd hh:mm", util.printd("yyyy/mm/dd ", new Date()) + this.getField("TimeIn1").value);

// create JavaScript date time object for end time
var oEnd = util.scand("yyyy/mm/dd hh:mm", util.printd("yyyy/mm/dd ", new Date()) + this.getField("TimeOut1").value);

// compute difference in hours from the difference in hours
var DiffHours = (oEnd.valueOf() - oStart.valueOf()) / 1000 / 60 / 60;
if (oEnd.valueOf() < oStart.valueOf()) DiffHours = (DiffHours + 24);
var WholeHours = Math.floor(DiffHours);

// allow users to enter "12:00am" and still calculate correctly
var RegExp = /12/;
var RegExp2 = /am/;
var TOText = (this.getField("TimeOut1").value);
if(RegExp.test(TOText))
if(RegExp2.test(TOText))
WholeHours = (WholeHours + 12);
var TIText = (this.getField("TimeIn1").value);
if(RegExp.test(TIText))
if(RegExp2.test(TIText))
WholeHours = (WholeHours + 12);

// make total "0" if there is no time entered in either TimeIn or TimeOut
if(TIText == "")
(WholeHours = 0)
if(TOText == "")
(WholeHours = 0)

// calculate hours from time (:60) to (1.00) and round off to quarter hour
var DecHours = util.printf("%,302.0f",((DiffHours % 1) * 4).toFixed()) * 25;
var HourTotal = (WholeHours) + "." + (DecHours);
if (DecHours == 100) event.value = Math.ceil(HourTotal);
else event.value = (HourTotal)

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use the format of the fields to help you with the display of data and control the input format of the data. You can also use document level functions to cut down on the amount of coding that has to be done. Their use will also make maintenance and debugging much easier. And if you break them into functions that perform a very specific general conversion, you will be able to use the code again in another form.

// document level functionsfunction Round2Nearest(fValue, fMin) {return (Math.round(fValue / fMin)) * fMin;} // end round 2 nearest function function TimeDiff(cInName, cOutName) {// Using a time format of 'h:MM tt' for the start and end times// create JavaScript date time object for start timevar oStart = util.scand("yyyy/mm/dd h:MM tt", util.printd("yyyy/mm/dd ", new Date()) + this.getField(cInName).value); // create JavaScript date time object for end timevar oEnd = util.scand("yyyy/mm/dd h:MM tt", util.printd("yyyy/mm/dd ", new Date()) + this.getField(cOutName).value); // adjust date for start time if start time is later than the end time to the previous dayif (oEnd.valueOf() < oStart.valueOf()) {oStart.setDate(oStart.getDate() - 1);} // end adjust start date // compute difference in millisecondsvar Diff = (oEnd.valueOf() - oStart.valueOf())// return difference in minutesreturn Diff} // end TimeDiff function DiffQtrHrs(fValue) {// round to nearest 15 minfValue = Round2Nearest(fValue, 15);// convert to hoursfValue = fValue / 60;// round to 1/100 th of an hourfValue = Round2Nearest(fValue, 0.01);// return computed difference in quarter hours as a decimalreturn fValue} // end DiffQtrHrs// end document level functions

Your custom calculation script then becomes:

event.value = DiffQtrHrs( TimeDiff('TimeIn1', 'TimeOut1') );

George Kaiser

sjsmith
Registered: Sep 2 2008
Posts: 28
Thanks for the fast response.

I used your script instead, but it still doesn't round the times off early enough.

I need the times to round before the total is calculated. For example: 9:10am to 10:22pm needs to calculate 13.00 hours. (9:10am rounds off to 9:15 and 10:22pm rounds off to 10:15, so 13.00.) Now it is returning 13.25 as it is totalling first, and then rounding off the result.

The script you provided is certainly cleaner than what I had, so I am sure there is a good way to remedy this. Any suggestions?
sjsmith
Registered: Sep 2 2008
Posts: 28
I played around with the TimeDiff function and fixed my problem:

function TimeDiff(cInName, cOutName) {
// Using a time format of 'h:MM tt' for the start and end times
// create JavaScript date time object for start time
var oStart = util.scand("yyyy/mm/dd h:MM tt", util.printd("yyyy/mm/dd ", new Date()) + this.getField(cInName).value);

// create JavaScript date time object for end time
var oEnd = util.scand("yyyy/mm/dd h:MM tt", util.printd("yyyy/mm/dd ", new Date()) + this.getField(cOutName).value);

// adjust date for start time if start time is later than the end time to the previous day
if (oEnd.valueOf() < oStart.valueOf()) {
oStart.setDate(oStart.getDate() - 1);
} // end adjust start date

// convert Times to minutes
var StartTime = (oStart.valueOf() / (1000 * 60));
var EndTime = (oEnd.valueOf() / (1000 * 60));
// round Times to nearest 15 min
StartTime = Round2Nearest(StartTime, 15);
EndTime = Round2Nearest(EndTime, 15);
// convert Times to hours
StartTime = StartTime / 60;
EndTime = EndTime / 60;
// round Times to 1/100 th of an hour
StartTime = Round2Nearest(StartTime, 0.01);
EndTime = Round2Nearest(EndTime, 0.01);
// compute difference in Times
Diff = (EndTime - StartTime)
// return computed difference in hours
return Diff
} // end TimeDiff
// end document level functions

Thank you very much for your help. This is also my first experience with Document scripts. This has been great stuff to learn.