Help needed for Time Calc Java Script
Copy link to clipboard
Copied
I am trying to create a timesheet in PDF and have hit a speed bump. Is there anyone there who could provide me with a script that calculated the difference between 2 times? (Field2 time)-(Feild 1 Time) = Duration.I am desperate and have tried everything.
I proccess the payroll for a company and all of the 500 or so timesheets that I received each fortnight are paper copies and I am pulling my hair out. I need to use Adobe forms (We have Adobe Pro 9 and I am using the XML forms, not live cycle) as the serach engine that we use for emails cannot search on excel documents.
I have no Java script experience and I am getting completely lost in the subject. Any help would be greatly appreciated.
Thanks anyone who can help at all.
-Sohnia
Copy link to clipboard
Copied
function Time2Num(sTime, cFormat) {
// convert a date time string to seconds since the epoch date
var oTime = util.scand(cFormat, sTime); // date time object
var fTime = oTime.valueOf(); // time string to milliseconds
return Math.round(fTime / 1000); // return rounded seconds
}
function TimeDiff(sEnd, sStart) {
// compute difference in time within same day as minutes
var fEnd = 0; // default value if not data
var fStart = 0; // default value if not data
var cDateFormat = "m/d/yyyy "; // date format string
var sNowDate = util.printd(cDateFormat, new Date()); // get today's date as "m/d/yyyy'
var cDateTimeFormat = cDateFormat + "h:MM tt";
// compute difference if we have data
if(sEnd != "" & sStart != "") {
// get end time in minutes
var cDateTimeValue = sNowDate + sEnd; // start date time string
var fEnd = Time2Num(cDateTimeValue, cDateTimeFormat) / 60; // convert to seconds and divide by 60 sec
// get start time in minutes
var cDateTimeValue = sNowDate + sStart; // start date time string
var fStart = Time2Num(cDateTimeValue, cDateTimeFormat) / 60; // convert to seconds and divide by 60 sec
} // end if we have value
return fEnd - fStart; // compute and return the difference in minutes
} // end function
function Mins2HrsMins(fMinutes) {
// convert seconds into formatted string of hours:minutes
var cDisplayFormat = "%01.0f" + ":" + "%02.0f"; // format for returned value
var fHrs = Math.floor(fminutes / 60); // compute whole hours from the difference
var fMins = fMinutes % 60; // get minutes less than 1 hour form the difference
return util.printf(cDisplayFormat, fHrs, fMins); // format and return computed value
} // end function
var fDiff = TimeDiff(this.getField("Field2 Time").value, this.getField("Field1 Time").value)
event.value = Mins2HrsMins(fDiff); // format computed value
The 3 functions can be placed as a document level functions. One only needs to call the "TimeDiff(sEnd, sStart)" with the end and start time strings to get the difference in minutes between the start and end times. This value can then be further processed to summ other differences. The "Sec2HrsMins()" will format a minutes value to hours and minutes if any additional processing is done.
Of one can use the following event script to compute the difference and format the result with one line of code:
event.value = Mins2HrsMins( TimeDiff(this.getField("Field2 Time").value, this.getField("Field1 Time").value) );
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Am I correct in assuming that I should use the same script but just a different variance coding to sum times? Could anyone please let me know what code I should use?
I need to keep the size of this timesheet as small as possible otherwise our IT area will not let me use it. Any help would be appreciated.
Thanks.
Copy link to clipboard
Copied
You code for each period would be similar to:
var fDiff = TimeDiff(this.getField("Field2 Time").value, this.getField("Field1 Time").value)
event.value = Mins2HrsMins(fDiff); // format computed value
And you would only need to change the field names as required.
To get the total for a series of fields one could use a custom calculation script similar to:
var fDiff = TimeDiff(this.getField("Field2 Time").value, this.getField("Field1 Time").value); // get first pair of times
fDiff += TimeDiff(this.getField("Field4 Time").value, this.getField("Field3 Time").value) // add next pair of times
fDiff += TimeDiff(this.getField("Field6 Time").value, this.getField("Field5 Time").value) // add next pair of times
fDiff += TimeDiff(this.getField("Field8 Time").value, this.getField("Field7 Time").value) // add next pair of times
fDiff += TimeDiff(this.getField("Field1- Time").value, this.getField("Field9 Time").value) // add last pair of times
event.value = Mins2HrsMins(fDiff); // format computed value
Or one could even fill in each day's time while computing the sum.
Copy link to clipboard
Copied
Hello GKaiseril,
I'm having a bit of a problem in doing this Time calculation and was wondering if you could help?
I've used your 3 functions as 'Document Javascripts' and using a custom calculation script
" event.value = Mins2HrsMins( TimeDiff(this.getField("Field2 Time").value, this.getField("Field1 Time").value) ); " on the "TotHours" field but for some reason the "TotHours" field appears blank.
I've also tired using all 3 functions and the event.value as a custom calculation script but still get the same result.
Could you please let me know what I am doing wrong?
Thanks for you help in advance!
Ken!
Copy link to clipboard
Copied
Ken,
what is the foramt style of you time string?
The script is for "h:mm tt" like 9:00 am.
As to using web page JavaScirpt, you will need to adjsut it for the Acrobat JavaScirpt variation.
Copy link to clipboard
Copied
Hi GKaiseril,
Thanks a million for your prompt reply!!
Both the "Field2 Time" and the "Field1 Time" fields have the time format of "h:MM tt" format.
I am using your javascript script on the beginning of this page (which you did for Sonhia), but the field displaying the difference between the two times - "TotHours" - is blank.
I am using these scripts in a pdf form in Acrobat X (on a Mac) - Could that be an issue?
Thanks again!
Copy link to clipboard
Copied
No that should not be a problem.
Are the field names correct?
Are the field names spelled correctly?
Are the field names capitalized correctly?
Is JavaScript enabled in Acrobat?
Copy link to clipboard
Copied
Here is the script I am using:
function Time2Num(sTime, cFormat) {
// convert a date time string to seconds since the epoch date
var oTime = util.scand(cFormat, sTime); // date time object
var fTime = oTime.valueOf(); // time string to milliseconds
return Math.round(fTime / 1000); // return rounded seconds
}
function TimeDiff(sEnd, sStart) {
// compute difference in time within same day as minutes
var fEnd = 0; // default value if not data
var fStart = 0; // default value if not data
var cDateFormat = "m/d/yyyy "; // date format string
var sNowDate = util.printd(cDateFormat, new Date()); // get today's date as "m/d/yyyy'
var cDateTimeFormat = cDateFormat + "h:MM tt";
// compute difference if we have data
if(sEnd != "" & sStart != "") {
// get end time in minutes
var cDateTimeValue = sNowDate + sEnd; // start date time string
var fEnd = Time2Num(cDateTimeValue, cDateTimeFormat) / 60; // convert to seconds and divide by 60 sec
// get start time in minutes
var cDateTimeValue = sNowDate + sStart; // start date time string
var fStart = Time2Num(cDateTimeValue, cDateTimeFormat) / 60; // convert to seconds and divide by 60 sec
} // end if we have value
return fEnd - fStart; // compute and return the difference in minutes
} // end function
function Mins2HrsMins(fMinutes) {
// convert seconds into formatted string of hours:minutes
var cDisplayFormat = "%01.0f" + ":" + "%02.0f"; // format for returned value
var fHrs = Math.floor(fminutes / 60); // compute whole hours from the difference
var fMins = fMinutes % 60; // get minutes less than 1 hour form the difference
return util.printf(cDisplayFormat, fHrs, fMins); // format and return computed value
} // end function
var fDiff = TimeDiff(this.getField("Field2_Time").value, this.getField("Field1_Time").value)
event.value = Mins2HrsMins(fDiff); // format computed value
All the above code is in the "Calculate>Custom Calculate Script" of the "TotHours" field.
I have the Javascript Debugger enabled in Acrobat and when I enter the time in "Field2_Time" and hit the tab key I get this in the Debugger:
fminutes is not defined
31:Field:CalculateException in line 31 of function Mins2HrsMins, script Field:Calculate
Exception in line 37 of function top_level, script Field:Calculate
ReferenceError: fminutes is not defined
31:Field:Calculate
I hope this helps.
Thank you so much!
Copy link to clipboard
Copied
Hello GKaiseril,
As I red over the last reply, I figured where the error was and corrected it (fminutes should have been fMinutes on line 31)and it now works.
Thank very much for helping out with this.
Ken!
Copy link to clipboard
Copied
And do the form field names agree with your form fields in spelling, capitalization, and spaces in the name?
Have you looked at the JavaScript console for any errors?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied

