Copy link to clipboard
Copied
I've been going through everything I can about how to create a script for timesheets but they are all so confusing.
I need a script to show the hours worked (see below) where I used HH:MM for the time in/time out.
I really appreciate any help provided!
Copy link to clipboard
Copied
There were quite a lot of issues with the code... Use this, instead:
function timeConvert(timeIn1, timeOut1) {
if (timeIn1=="" || timeOut1=="") {
event.value = "";
} else {
var sT = timeIn1.split(":");
var eT = timeOut1.split(":");
var t1 = (Number(sT[0]) * 60) + Number(sT[1]);
var t2 = (Number(eT[0]) * 60) + Number(eT[1]);
var num = t2-t1;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes) / 60;
var total = (rhours + rminutes);
if (total<0) total+=24;
event.value = total.toFixed(2);
}
}
timeConvert(this.getField("TIMEIN1").valueAsString, this.getField("TIMEOUT1").valueAsString);
Also, your initial example is incorrect. From 17:00 to 5:25 it's not 10.25 hours, but 12.42.
Copy link to clipboard
Copied
That photo doesn't show fields calculation order.
To set field calculation order, select 'Prepare form' tool, click on 'More' and then click on 'Set field calculation order',
fields that calculate first should be on top.
Copy link to clipboard
Copied
There are a LOT of code examples in this forum for this task, but first you need to clearly define it. In the first row, for example, the time span goes over midnight. So does that mean the working period can span multiple days? Can it be longer than 24 hours?
Copy link to clipboard
Copied
17:00 to 05:25 would be from 5:00 PM to 5:15 AM (say Friday night to Saturday morning) so that would be 12 hours. Individual could work up to 16-18 hours but nothing over 24 hours.
Copy link to clipboard
Copied
Try searching for "time calculation" or "worksheet calculation" and you'll find many threads. Here's one example: https://community.adobe.com/t5/acrobat-discussions/timesheet-difference-time-calculation-timein-time...
Copy link to clipboard
Copied
I looked at that example before but didn't understand it. Below is an example of the names of my fields:
Using the example you provided, I tried editing it to see if I could make it work for me, however, it did not. See what I put in Custom Calculation Script:
function timeConvert(timein1,timeout1) {
var sT = TimeIn1.split(":");
var eT = TimeOut1.split(":");
var t1 = (Number(sT[0])*60)+Number(sT[1]);
var t2 = (Number(eT[0])*60)+Number(eT[1]);
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes)/60;
if(!timein1 || !timeout1)
event.value = "";
else
event.value = rhours + rminutes;}
Your assistance is greatly appreciated!
Copy link to clipboard
Copied
There were quite a lot of issues with the code... Use this, instead:
function timeConvert(timeIn1, timeOut1) {
if (timeIn1=="" || timeOut1=="") {
event.value = "";
} else {
var sT = timeIn1.split(":");
var eT = timeOut1.split(":");
var t1 = (Number(sT[0]) * 60) + Number(sT[1]);
var t2 = (Number(eT[0]) * 60) + Number(eT[1]);
var num = t2-t1;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes) / 60;
var total = (rhours + rminutes);
if (total<0) total+=24;
event.value = total.toFixed(2);
}
}
timeConvert(this.getField("TIMEIN1").valueAsString, this.getField("TIMEOUT1").valueAsString);
Also, your initial example is incorrect. From 17:00 to 5:25 it's not 10.25 hours, but 12.42.
Copy link to clipboard
Copied
I get the below error when I entered the above script:
function timeConvert(timeIn1, timeOut1) { if (timeIn1=="" || timeOut1=="") { event.value = ""; } else { var sT = timeIn1.split(":"); var eT = timeOut1.split(":"); var t1 = (Number(sT[0]) * 60) + Number(sT[1]); var t2 = (Number(eT[0]) * 60) + Number(eT[1]); var num = t2-t1; var hours = (num / 60); var rhours = Math.floor(hours); var minutes = (hours - rhours) * 60; var rminutes = Math.round(minutes) / 60; var total = (rhours + rminutes); if (total<0) total+=24; event.value = total.toFixed(2); } } timeConvert(this.getField("TIMEIN1").valueAsString, this.getField("TIMEOUT1").valueAsString);
Copy link to clipboard
Copied
Copy link to clipboard
Copied
The error was on my end.
Thank you so much!!!!!!!
Copy link to clipboard
Copied
I thought this was working but apparently not.
I have timesheet, where time is entered, and hours worked are calculated.
Then the time goes into the Total Yearly Hours Worked field and is then subtracted from the Hours Allotted for Total Hour Left to Work (see below).
However, the 5.00 hours worked (line 1) do not appear in the Total Yearly Hours Worked field until hours are entered into the Time In field (line 2).
The script I have for the Hours Worked is:
function timeConvert(timeIn1, timeOut1) {
if (timeIn1=="" || timeOut1=="") {
event.value = "";
} else {
var sT = timeIn1.split(":");
var eT = timeOut1.split(":");
var t1 = (Number(sT[0]) * 60) + Number(sT[1]);
var t2 = (Number(eT[0]) * 60) + Number(eT[1]);
var num = t2-t1;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes) / 60;
var total = (rhours + rminutes);
if (total<0) total+=24;
event.value = total.toFixed(2);
}
}
timeConvert(this.getField("TIMEIN1").valueAsString, this.getField("TIMEOUT1").valueAsString);
I have the following to add the time to the Total Yearly Hours Worked field:
Hopefully this makes sense and if so can someone assist me with this issue?
Thank you, Valerie
Copy link to clipboard
Copied
Check, 'field calculation order'.
Copy link to clipboard
Copied
I thought of this too but it doesn't fix the issue:
Copy link to clipboard
Copied
That photo doesn't show fields calculation order.
To set field calculation order, select 'Prepare form' tool, click on 'More' and then click on 'Set field calculation order',
fields that calculate first should be on top.
Copy link to clipboard
Copied
Thank you so much, this fixed the issue!
I didn't know there was a "set field calculation order" under More for Prepare Form tool.
Good information to know.