Skip to main content
Known Participant
March 5, 2022
Answered

TimeSheet Help in Adobe Acrobat PDF as In the Excel

  • March 5, 2022
  • 1 reply
  • 7045 views

Hi I am new in this field. I have created a worksheet in EXCEL that calculates total hours worked. It is working both for day time or midnight time. See screenshot.

I want to create the similar workshet in ADOBE Acrobat PDF Form. I have tried many scripts from here but wsn't successful.

Say I have two fields, "Time In" and "Time Out", Kindly help me how I can do this?

Time calculated should be in HH:MM format.

Your help means a lot to me. 

Thans & Regards,

Muasher 

This topic has been closed for replies.
Correct answer try67

I want a result with HH:MM format. For example if I write Time in 02:00 and Timr Out 04:00 the Total Time should result as 02:00 not 2.0

 


Use this code as a doc-level 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;
	}
}

 

Then call it like this (for example):

 

calcTimeDiff("D5", "E5");

1 reply

BarlaeDC
Community Expert
Community Expert
March 7, 2022

Hi,

 

Hopefully, this can help, if you are ablet o get a script running but it is not quite correct, then you can post that script and what is not working for you and we can investigate.

 

https://community.adobe.com/t5/acrobat-discussions/need-help-creating-timesheet-in-adobe-acrobat/td-p/11901315

Known Participant
March 9, 2022

Thanks for the response. But I have applied this code but stil it counts time in decimals. The code I used in Calculate< Custom Calculation Script is,

 

var start = this.getField("Time 3.1").valueAsString;
var finish = this.getField("Time 4.1").valueAsString;

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

var hourDiff = finishArr[0] - startArr[0];
var minDiff = Math.floor(((finishArr[1] - startArr[1]) / 60)*100);
if (minDiff<0) {hourDiff--; minDiff += 100;}
if (hourDiff<0) hourDiff+=24;
if (minDiff.toString().length == 1) minDiff = '0' + minDiff;
event.value = hourDiff + "." + minDiff;
}
var nHrs = Math.floor(nElapsed / 60); // whole hours;

// elapsed minutes

var nMins = nElapsed % 60; // remainder minutes;

// format into hours and minutes;

event.value = util.printf("%,101.0f:%,102.0f", nHrs, nMins);

Bernd Alheit
Community Expert
Community Expert
March 9, 2022

At util.printf you uses the format for floating numbers.