Timesheet Calculation Javascript

New Here ,
Jul 17, 2022 Jul 17, 2022

Copy link to clipboard

Copied

Hello there, I know that there are multiple chains on this but I am struggling to combine a few aspects of the timecode. Inserting a picture. Essentially trying to write a script for the difference between hours worked minus a lunch break. Also trying to have the calculation boxes come up as blank if no time is input. (Also trying to avoid having lunch in and out times)

 

This is what I have - can someone advise? A quick solution would be much appreciated

 

// start
var start = this.getField("1").valueAsString
var startArr = start.split(":");

// finish
var finish = this.getField("3").valueAsString;
var finishArr = finish.split(":");

// lunchbreak
var lunchbreak =this.getField("2").valueAsString
var lunchbreakArr = finish.split(":")

//blanknotfilled
if (start=="" || finish=="") event.value = ""
else {
// difference
var hourDiff = (Math.abs(finishArr[0] - startArr[0])) - lunchbreakArr[0];
var minDiff = Math.floor(((Math.abs(finishArr[1] - startArr[1])- lunchbreakArr[0])/ 60)*100);
if (minDiff.toString().length == 1)
minDiff = '0' + minDiff;
var output = hourDiff + "." + minDiff;}

TOPICS
JavaScript , PDF forms

Views

70

Likes

Translate

Translate

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 guidelines

correct answers 1 Correct answer

Community Expert , Jul 18, 2022 Jul 18, 2022

You can use this as 'Document level script':

function timeConvert(time1,time2,time3) {
var aTime = time1.split(":");
var bTime = time2.split(":");
var cTime = time3.split(":");
var a = (Number(aTime[0])*60)+Number(aTime[1]);
var b = (Number(bTime[0])*60)+Number(bTime[1]);
var c = (Number(cTime[0])*60)+Number(cTime[1]);
if(time2)
var num = c-a-b;
else
num = c-a;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes)/60;
if(!
...

Likes

Translate

Translate
Community Expert ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

You can use this as 'Document level script':

function timeConvert(time1,time2,time3) {
var aTime = time1.split(":");
var bTime = time2.split(":");
var cTime = time3.split(":");
var a = (Number(aTime[0])*60)+Number(aTime[1]);
var b = (Number(bTime[0])*60)+Number(bTime[1]);
var c = (Number(cTime[0])*60)+Number(cTime[1]);
if(time2)
var num = c-a-b;
else
num = c-a;
var hours = (num / 60);
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.round(minutes)/60;
if(!time1 || !time3)
event.value =  "";
else
event.value = rhours + rminutes;}

and then call it from total hour's field custom calculation script like this:

timeConvert(this.getField("1").valueAsString,this.getField("2").valueAsString,this.getField("3").valueAsString);

 

Likes

Translate

Translate

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 guidelines
New Here ,
Jul 18, 2022 Jul 18, 2022

Copy link to clipboard

Copied

THANK YOU!! This is exactly what I was looking for and is a very clear. Really appreciate your help and quick response time.

Likes

Translate

Translate

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 guidelines
New Here ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

LATEST

Hello there, had one other question on this solution above. I often have people work past midnight, which seems not to work with this form. I can have people enter 25:00 for 1am, but this still comes up as invalid. Curious what is causing this and if there is an easy fix?

Likes

Translate

Translate

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 guidelines