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

TimeSheet Difference Time Calculation TimeIn, TimeOut, BreakTime

Explorer ,
May 11, 2023 May 11, 2023

Hi, I have checked many posts but coudn't understand the solution. 

 

I am trying to create a TimeSheet calculator with fields TimeIn, TimeOut, BreakTime, Total Hours

 

Total Hours should calculate Time Difference if TimeIn, TimeOut and deduct BreakTime. 

 

For Example, TimeIn= 4:00     TimeOut= 14:00 and BreakTime= 12:00-13:00


TotalHours= 9:00 

 

Please guide me about it. 

 

I really appreciate Adobe Community. Thanks

TOPICS
How to , JavaScript , PDF , PDF forms
2.9K
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
1 ACCEPTED SOLUTION
Community Expert ,
May 12, 2023 May 12, 2023

Try this:

function timeConvert(startTime,endTime,breakTime) {
var sT = startTime.split(":");
var eT = endTime.split(":");
var bT = breakTime.split(":");
var t1 = (Number(sT[0])*60)+Number(sT[1]);
var t2 = (Number(bT[0])*60)+Number(bT[1]);
var t3 = (Number(eT[0])*60)+Number(eT[1]);
if(breakTime)
var num = t3-t1-t2;
else
num = t3-t1;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes)/60;
if(!startTime || !endTime)
event.value =  "";
else
event.value = rhours + rminutes;}

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
Enthusiast ,
May 11, 2023 May 11, 2023

There are a lot of answered posts about this question on the forum, if you didn't understand any of them, then guiding you will not help. You can always hire someone to do it for you, though.

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 ,
May 12, 2023 May 12, 2023

I have this code working for Time In and Time Out difference. But can you just modify it so that Break Time is deducted.

Document Script;

function calcTimeDiff(startFieldName, endFieldName) {
var startField = this.getField(startFieldName);
var start = startField.valueAsString;
var endField = this.getField(endFieldName);
var end = endField.valueAsString;

if (start=="" || end=="") event.value = "";
else {
var startArr = start.split(":") ;
var finishArr = end.split(":") ;

var hourDiff = Number(finishArr[0]) - Number(startArr[0]);
var minDiff = Number(finishArr[1]) - Number(startArr[1]);
if (minDiff<0) {hourDiff--; minDiff += 60;}
var minDiffString = minDiff.toString();
if (minDiffString.length==1) minDiffString = "0" + minDiffString;
event.value = hourDiff + ":" + minDiffString;
}
}

 

Custom Calculation Script;

calcTimeDiff("Time inRow1_2", "Time outRow1_2");

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 ,
May 12, 2023 May 12, 2023

Try this:

function timeConvert(startTime,endTime,breakTime) {
var sT = startTime.split(":");
var eT = endTime.split(":");
var bT = breakTime.split(":");
var t1 = (Number(sT[0])*60)+Number(sT[1]);
var t2 = (Number(bT[0])*60)+Number(bT[1]);
var t3 = (Number(eT[0])*60)+Number(eT[1]);
if(breakTime)
var num = t3-t1-t2;
else
num = t3-t1;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes)/60;
if(!startTime || !endTime)
event.value =  "";
else
event.value = rhours + rminutes;}
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 ,
May 12, 2023 May 12, 2023

Thanks for sharing this. 

Should I use this code as a Doc Script, or in Custom calc script? 

Do I need to change the fields names to 

startTime,endTime,breakTime

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 ,
May 12, 2023 May 12, 2023

Use it in Document script, then call function from calculation script in field where you want to show hours total.

Call function like this:

var a = this.getField("Start time field name").valueAsString;
var b = this.getField("End time field name").valueAsString;
var c = this.getField("Break time field name").valueAsString;
timeConvert(a,b,c);

 

Replace 'Orange' text with your actual field names.

 

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 ,
May 12, 2023 May 12, 2023
LATEST

It's working now. Thanks you so much. 

But it is showing the result in decimals. Is there a way to show total in HH:mm format?

Also. if there is an option to add AM, PM format? if it's complicated then don't worry about it. 

Thanks a lot 

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