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

Subtracting time- either javascript or simplified field notation

Krewl2u
Registered: Mar 16 2008
Posts: 3

I am trying to make a timesheet and the only thing I cannot figure out is how to subtract time to hours. Sounds simple but somehow I have not managed to do it.

My time sheet is basically calculated for Monday-Friday and the workers work once in the morning and once in the afternoon so I have two fields for each day. From1 ,To1 for the morning and From2,To2 for the afternoon. So I want to be able to calculate the hours from the morning and the afternoon and come up with hours for the day.
I tried looking for examples to get me through it but could not find it for javascript or simplified field notation.

Any help would be greatly appreciated.

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you have studied the samples, you will see there the use JavaScripts methods or Acrobat funcitons that are not supported by the "Simplified Fied notaion", so you will have to use the "Custom java script calculation" .

The basic calculation for the morning hours is:

var sIn = this.getFiel("From1").value;
var sOut = this.getFiel("To1").value;


var fStart = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sIn)) / Min);
var fEnd = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sOut)) / Min);
var diff = 0;
if (fStart > fEnd) {
app.alert("The start time must be before the end time!");
}
else diff = Math.floor(fEnd - fStart);


event.value = Number(util.printf("%,101d", (diff / 60))) + Number((util.printf("%,102d", (diff % 60))) / (60 * 100));

Since this calculation in a generalized way will be repeated 10 times, you should use a document level functions to perform the calculations:


function PFDiff_HrMin(sIn, sOut)
{
/*
Purpose: compute the difference between the inputted time strings, hh:mm, into minutes

Inputs:
sIn - starting time in the hh:mm format
sOut - ending time in the hh:mm format

Returns: the difference in minutes or zero if invalid
*/

var fStart = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sIn)) / Min);
var fEnd = Math.floor( Number(util.scand("mm/dd/yyyy hh:mm", "01/01/1970 " + sOut)) / Min);
var diff = 0;
if (fStart > fEnd) {
app.alert("The start time must be before the end time!");
}
else diff = Math.floor(fEnd - fStart);
return diff;
}

function Min2HrsMin (sMin) {
return util.printf("%,101d", (sMin / 60)) + ":" + util.printf("%,102d", (sMin % 60));
}

function Min2DecHrs (sMin) {
/*
Purpose: to convert the passed minutes into hours and decimal hours, 30 minutes = 0.5

Inputs:
sMin - minutes

Returns hours and minutes as a decimal number
*/
return Number(util.printf("%,101d", (sMin / 60))) + Number((util.printf("%,102d", (sMin % 60))) / (60 * 100));
}

The custom calculation for the first day will be:

// the field number for the morning time: 1, 3, 5, 7 ,9
var Day = 1

// compute the morning minutes
var DayMorning = PFDiff_HrMin(this.getFeild("From" + Day).value, this.getFeild("To" + Day).value)

// compute the afternoon minutes
var DayAfternoon = PFDiff_HrMin(this.getFeild("From" + Day + 1).value, this.getFeild("To" + Day + 1).value)

// add the morning and afternoon time and display the total hours
event.value = Min2NrsMin(DayMorning + DayAfternoon);

George Kaiser

Krewl2u
Registered: Mar 16 2008
Posts: 3
Thank you for your assistance as I am new to acrobat and javascript. I will try and get this going this evening.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
var Min = 1000 * 60;
var sIn = this.getFiel("From1").value;
var sOut = this.getFiel("To1").value;

...and


var Min = 1000 * 60;
function PFDiff_HrMin(sIn, sOut)
{
...

George Kaiser

mcfar56
Registered: Mar 10 2011
Posts: 6
Wow, I am lost. I just want to subtract two time fields to get the result. Time in 09:15 out 10:00 and get the result of 0:45 (45 min) then add all of the results to get total hrs and min for the day. I never realized how challenging this would be. I don't even know where to start. Help, Rich
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi mcfar56,

A good place to start would be with these two tutorials on working with dates and times from the Learning Center here-

Working with Dates and Times in Acrobat JavaScript Part 1
[br]
Working with Dates and Times in Acrobat JavaScript Part 2[br]

Part 2 has a download sample file with code you can examine.

Hope this helps,

Dimitri
www.pdfscripting.com
www.windjack.com


try67
Expert
Registered: Oct 30 2008
Posts: 2398
I developed a script to help with this kind of situations:
http://try67.blogspot.com/2011/03/acrobat-calculate-time-differences-in.php

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

mcfar56
Registered: Mar 10 2011
Posts: 6
Dimitri, Thank you. I am new to java but not to programming. I have some grasp but this for some reason has evaded me. Try67 offered to help, for a price, but between a few friends and gkaiseril I have been able to complete my task, almost. I have a few more problems that I am working on. I have created a trip log for our technicians in a PDF form that they fill out in the field. The last thing I need is to add all of the times (time on job) together. It seems easy but maybe my age is messing me up. Funny, I have been programming since the TI99 in the 80's. I gave it up for about 6 years and decided to try again. Now, it's not sticking... bummer, Rich