Skip to main content
Known Participant
April 26, 2021
Answered

Javascript - Subtracting from a total field

  • April 26, 2021
  • 1 reply
  • 1269 views

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.

 

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 🙂

 

This topic has been closed for replies.
Correct answer Asim123

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.

1 reply

Inspiring
April 26, 2021

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

Known Participant
April 26, 2021

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;

}

 

Asim123Correct answer
Inspiring
April 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.