Javascript - Subtracting from a total field

Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Hi Community

I'm in a bit of a pickle with a subtract function in my pdf form.
I need to make a piece of subtraction code that subtracts break time from a total time.

 

Jakob5FD0_0-1619428610959.png

This is the form i'm working on.

 

This is the code snipped i'm working on. It is taken from the field totalTime.1

 

indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+indice).value;
var sBreak=this.getField("BreakRow."+indice).value;

if(sStart != "" & sEnd != "" & sBreak != "") {
	var fStart=Time2Num("hh:mm",sStart);
	var fEnd=Time2Num("hh:mm",sEnd);
        var fBreak=Time2Num ("hh.mm",sBreak);
	var fDiff=fEnd-fStart-fBreak;
	
if (fDiff<0) fDiff=(24*3600)+(fEnd-fStart-fBreak);
	fDiff=Math.round(fDiff/60);
	fHours=Math.floor(fDiff/60);
	fMins=((fDiff/60)-fHours )*60;
	sMins=util.printf("%,302.0f",fMins);
	event.value=fHours+":"+sMins;
}

 

This is one of the solutions i've tried thus far, though to no avail. I've also tried to create the subtraction as a seperate if statement below the fDiff if statement, though i'm afraid that i lack the skills to spot where i go wrong. Can anybody help me 🙂

 

TOPICS
How to, JavaScript

Views

139

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

Engaged , Apr 26, 2021 Apr 26, 2021
So then you just want to substract total from break? and show in what field(s)?You have errors in naming fields break row missing a . (dot) in your photo.

Likes

Translate

Translate
Engaged ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

So you just want to use loop to go through fields and substract "end-start-break" and show as decimal in "totaltime"?

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
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

I want to create a function or statement that subtracts the break value from the totalTime, when the break value is changed. 🙂

 

I have tried this solution, still to no avail.

indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+indice).value;
var sBreak=This.getField("BreakRow."+indice).value;
var sTotal=This.getField("totalTime."+indice).value;

if(sStart != "" & sEnd != "") {
	var fStart=Time2Num("hh:mm",sStart);
	var fEnd=Time2Num("hh:mm",sEnd);
	var fDiff=fEnd-fStart;
	
if (fDiff<0) fDiff=(24*3600)+(fEnd-fStart);
	fDiff=Math.round(fDiff/60);
	fHours=Math.floor(fDiff/60);
	fMins=((fDiff/60)-fHours )*60;
	sMins=util.printf("%,302.0f",fMins);
	event.value=fHours+":"+sMins;
}

if (sBreak != ""){
    event.value = sTotal-sBreak;

}

 

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
Engaged ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

So then you just want to substract total from break? and show in what field(s)?

You have errors in naming fields break row missing a . (dot) in your photo.

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
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

Reverse actually. I wanna subtract break from total, so that the total worked hours will appear in totalTime (v).

Right now it subtracts the endingTime (y) with the startingTime (x) to calculate the work hours of the day. That part works fine.

 

Now i need to implement a break(z) function that subtracts the time employees went on a break, from the total time in totalTime.

 

i think the math would be like this:

x-y = v

v-z = new/updated v

 

Does that make sense?

 

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
Explorer ,
Apr 26, 2021 Apr 26, 2021

Copy link to clipboard

Copied

LATEST

I can happily tell that everything works now. I went back and used my first code, after i saw your point about me forgetting the . in the BreakRow.

 

Of course it didn't register anything with that typo. Everything works as it should now. Many thanks for taking your time to point out my mistakes 😄

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