Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
2

Timesheet

Explorer ,
Apr 11, 2024 Apr 11, 2024

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.

Timesheet.jpg

I really appreciate any help provided!

TOPICS
JavaScript , PDF forms
2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
2 ACCEPTED SOLUTIONS
Community Expert ,
Apr 13, 2024 Apr 13, 2024

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.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 01, 2024 Sep 01, 2024

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.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2024 Apr 12, 2024

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2024 Apr 12, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 12, 2024 Apr 12, 2024

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...

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2024 Apr 13, 2024

I looked at that example before but didn't understand it.  Below is an example of the names of my fields:

TimeIn-TimeOut.png

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2024 Apr 13, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2024 Apr 13, 2024

I get the below error when I entered the above script:

Error.jpg

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);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2024 Apr 13, 2024

Works fine for me. See attached.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2024 Apr 13, 2024

The error was on my end.

 

Screenshot 2024-04-13 143820.png

Thank  you so much!!!!!!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 30, 2024 Aug 30, 2024

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).

Valerie03_3-1725056733377.png

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:

Valerie03_4-1725056765066.png

Hopefully this makes sense and if so can someone assist me with this issue?

Thank you, Valerie

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 30, 2024 Aug 30, 2024

Check, 'field calculation order'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 01, 2024 Sep 01, 2024

I thought of this too but it doesn't fix the issue:

Valerie03_0-1725210137019.png

Valerie03_1-1725210192176.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 01, 2024 Sep 01, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 01, 2024 Sep 01, 2024
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines