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

Calculations using times

headsortails
Registered: Mar 17 2009
Posts: 6

Hi,

I'm trying to create a time sheet form with the daily and weekly hour totals be automatically calculated and entered.

I have a 5 column table:-

Start Time|Lunch Start|Lunch Finish|Finish Time|Total

I'm looking to calculate the daily total then to sum the total column to give a weekly total.

I think I'd be looking at a simple formula like: total=(finish time-start time)-(lunch finish-lunch start).

This calculates fine if they are simple numbers but when entered as a time (e.g. 13:45) then all goes pear-shaped.

Can anyone offer any advice as to how I may progress with this?

Thanks in advance,

Matt.

My Product Information:
LiveCycle Designer, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can write a function to split the time character string and convert the split string to minutes. You can then do the addition of the minutes for a totl minutes worked, from the total minutes worked you can compute the hours and minutes and create the formatted result string.

George Kaiser

headsortails
Registered: Mar 17 2009
Posts: 6
Many thanks for your prompt reply.

I guess you can guess my next question?

How would I go about doing that?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can use the following JavaScirpt functions to convert a time string as "HH:MM" to minutes and convert minutes to a formatted string of 'HH:MM".
function sTime2Min(sTime) {// convert format of HH:MM to minutesvar aTime = sTime.split(":");// fill if minutes are zeroif(aTime.length < 2) sTime[1] = 0;// return time string as number of minutesreturn (60 * Number(aTime[0])) + Number(aTime[1]);} // end sTime2Min function function Min2HrsMins(fMins) {// convert minutes to HH:MM formatvar fHrs = Math.floor(fMins / 60);var fMins = fMins % 60;var sHrs = util.printf("%,302d", fHrs);var sMins = util.printf("%,302d", fMins);return sHrs + ":" + sMins;} // end Min2HrsMins

You can then use the following JavaScript to calculate a difference between 2 times and display the results as 'HH:MM":
// calculate elapsed time between the fields named EndTime and StartTimevar fEtime = sTime2Min(EndTime.formattedValue) - sTime2Min(StartTime.formattedValue);// format the elapsed minutes as 'HH:MM"$.rawValue = Min2HrsMins(fEtime);

George Kaiser

FlyGirl
Registered: Apr 2 2009
Posts: 10
I'm having a terrible time with this. I'm trying to figure out how to display the total duty time by subtracting DutyOff - DutyOn. This is being done in a table in a cell named Duty. I can get the subtraction to work fine, but of course it's not in time, it's a regular number. I've tried some of the codes provided by users on this site and others, but nothing is working. Where do I put the codes? Calulate? Validate? How do I get it to work so it's displaying them HH:MM and doesn't go above 60? It's getting frustrating and I'm sure it's a pretty simple solution, but I'm not too familiar with all this yet.

Thanks for any help provided.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
For LiveCycle Designer.

The functions need to be placed as a form level script object. The calculation script is placed in the elapsed time field.

You can also use these functions to sum a series of elapsed time fields and format the minutes result as "HH:MM".

Edited to add;

The time string conversion assumes that the end and start times all occur within the same day. If not, you will need to include the the date of the start time and the date of the end time and perform a conversion to the date time object and then work with the time in milliseconds from the Epoch date.

George Kaiser

beaumisbro
Registered: Apr 6 2009
Posts: 14
I have a similar issue as the others but since i'm new to javascript, i could use your expert advice.
I have a Time In box (TI1) and a Time out box (TO1). both are formatted to use military time.
The hours worked (HW1) box needs to calculate the hours worked in decimals, meaning instead of saying 1hr 30 mins, i need it to say 1.5hrs.
i have the following code so far, please help a struggling newbie.

var stime = this.getField("TI1").value;
var etime = this.getField("TO1").value;
var diff = TO1.valueOf() - TI1.valueOf();

thank you kindly,
-Andy
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
As like in [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=19419]Calculate age in javascript], you need to use JavaScript's Date Time object to obtain the number of milliseconds since the Epoch date and you can then perform the necessary math to get the the number of hours in the difference. Also see the series [url=http://www.acrobatusers.com/tutorials/2006/date_time_part1]Working with date and time in Acrobat JavaScript (Part 1 of 3)] by Thom Parker.

You will have to use the "util.scand()" method with the appropriate format string to convert your times to the date time object.

// open the debugging consoleconsole.show();console.clear(); // get the values for the fieldsvar stime = this.getField("TI1").value;var etime = this.getField("TO1").value; // display some infromationconsole.println('TI1 - value: ' + stime + ' type of data: ' + (typeof stime) );console.println('TO1 - value: ' + etime + ' type of data: ' + (typeof etime) );console.println('etime - stime: ' + (etime - stime) ); // perform calculations only if we have valuesif(stime != '' & etime != '') {// convert time strings to date time object - pluging in a starting datevar oStime = util.scand('HH:MM', '1/1/1970 ' + stime);var oEtime = util.scand('HH:MM', '1/1/1970 ' + etime); // display date time objectsconsole.println('stime object contents: ' + oStime)console.println('etime object contents: ' + oEtime) // convert date time objects to valuesvar fStime = oStime.valueOf();var fEtime = oEtime.valueOf(); // values from date time objectconsole.println('oStime value: ' + fStime);console.println('oEtime value: ' + fEtime); // do some mathvar diff = fEtime - fStime;console.println('difference in values: ' + diff);console.println('difference in seconds: ' + (diff / 1000) );console.println('difference in minutes: ' + (diff / (1000 * 60)) );console.println('difference in hours: ' + (diff / (1000 * 60 * 60)) ); // display the resultsevent.value = diff / (1000 * 60 * 60);} // end if no missing data

You may need to use some additional JavaScripting to provide additional formatting of the result for leading zeros.

The above code is for AcroForms and not LiveCycle Designer, although with the appropriate changes in syntax it can be used in LiveCycle Designer as a JavaScript calculation.

George Kaiser

beaumisbro
Registered: Apr 6 2009
Posts: 14
wow you guys are amazing!!! for the end result formatting i simply formatted the HW1 box as a number with 2 decimal places. it seems to serve the purpose. thank you again, you've been a great help.
-Andy