Skip to main content
Participating Frequently
May 11, 2023
Answered

TimeSheet Difference Time Calculation TimeIn, TimeOut, BreakTime

  • May 11, 2023
  • 1 reply
  • 2793 views

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

This topic has been closed for replies.
Correct answer Nesa Nurani

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

1 reply

Inspiring
May 12, 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.

Participating Frequently
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");

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

 


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