Skip to main content
Participant
February 9, 2023
Answered

Count Hours/Mins between Start & End Time + Hours over 37.5 spill into OT Hours Field

  • February 9, 2023
  • 2 replies
  • 1251 views

Hi All, 

 

Have read lots of different posts across the forums to try and help me understand what I need in terms of JS in Acrobat Pro, however I am struggling to find the golden nugget. 

 

My issue is two parts:

 

1. A simple formula/JS to calculate the start time/end time between two fields (HH:MM format) and have that total displayed in the field marked 1 (TOTAL HOURS). 

 

2. Once the timesheet rows total over 37.5 hours in the 'NORMAL HOURS' field, the additional 'hours' spill into the 'OVERTIME HOURS' field. Eg. Total hours on the time sheet form is 45 hours, therefore 37.5 hours would be in the NORMAL HOURS field and 7.5 hours would be in the OVERTIME HOURS field. 

 

Screenshot attached. 

 

Any help on this would be really appreciated. 

Thanks! 

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

I would suggest you to use function at document level script, something like this:

function timeConvert(f,f1) {
var start = f.split(":");
var finish = f1.split(":");

var a = (Number(start[0])*60)+Number(start[1]);
var b = (Number(finish[0])*60)+Number(finish[1]);
var num = (b-a);
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
if(f&&f1)
return rhours + ":" + rminutes;
else
return "";}

Then just call function from total hours fields:

var timestarted = this.getField("START.TIMERow1").value;
var timefinished = this.getField("END.TIMERow1").value;
event.value = timeConvert(timestarted,timefinished);

For other total hours, use same, just change field names.

 

2 replies

Participant
February 9, 2023

Hi Nesa - Thanks for your response. Noted about the second part, I will try the validation once I get the time calculation. 

 

I was trying the following however I am getting a NaN:NaN result...

 

var timefinished = this.getField("END.TIMERow1").value;
var timestarted = this.getField("START.TIMERow1").value;

var datetimefinished = new Date('1970/01/01' + timefinished);
var datetimestarted = new Date('1970/01/01' + timestarted);

var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;

// calculate hours
var hours = Math.floor(difflnMilliSeconds / 3600) % 24;
difflnMilliSeconds -= hours *3600;

// calculate minutes
var minutes = Math.floor(difflnMilliSeconds / 60) % 60;
difflnMilliSeconds -= minutes * 60;

// set field value to the difference
event.value = hours + ":" + minutes;
Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
February 9, 2023

I would suggest you to use function at document level script, something like this:

function timeConvert(f,f1) {
var start = f.split(":");
var finish = f1.split(":");

var a = (Number(start[0])*60)+Number(start[1]);
var b = (Number(finish[0])*60)+Number(finish[1]);
var num = (b-a);
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes);
if(f&&f1)
return rhours + ":" + rminutes;
else
return "";}

Then just call function from total hours fields:

var timestarted = this.getField("START.TIMERow1").value;
var timefinished = this.getField("END.TIMERow1").value;
event.value = timeConvert(timestarted,timefinished);

For other total hours, use same, just change field names.

 

Participant
February 9, 2023

Thank you so much!

Nesa Nurani
Community Expert
Community Expert
February 9, 2023

There are a lot of time scripts on the forum here is one example:

https://community.adobe.com/t5/acrobat-discussions/adding-times-in-hh-mm-to-get-total-time-in-hh-mm/m-p/12095115#M315615 

For 'NORMAL HOURS' and 'OVERTIME HOURS' you can use validation script in 'NORMAL HOURS' to check if value is greater than 37.5 and subtract 37.5 from current value if it's greater, and set value to 37.5 and set 'OVERTIME HOURS' to be the result of subtract calculation (difference).