• Global community
• Language:
• Deutsch
• English
• Español
• Français
• Português
• 日本語コミュニティ
Dedicated community for Japanese speakers
• 한국 커뮤니티
Dedicated community for Korean speakers
Exit
• Count Hours/Mins between Start & End Time + Hours ...

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

Community Beginner ,
Feb 09, 2023 Feb 09, 2023

Copied

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!

TOPICS
Acrobat SDK and JavaScript

Views

706

Report

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

Community Expert , Feb 09, 2023 Feb 09, 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

...

5 Replies 5
Community Expert ,
Feb 09, 2023 Feb 09, 2023

Copied

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

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

Report

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

Copied

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;

Report

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

Copied

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.

Report

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

Copied

LATEST

Thank you so much!

Report

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