• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to include a break/subtract function in my timesheet

Explorer ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

I'm trying to implement a "break" function in my adobe acrobat js, which needs to subtract the break time with the total of workhours each day. This in turn needs to be reflected in the overall total which is a multiplication of every total hours of the game.

 

I've been looking everywhere for a solution, though i must admit that i have a hard time finding any. Could you guys assist me in finding what i'm looking for?

 

//<Document-Level>
//<ACRO_source>init</ACRO_source>
//<ACRO_script>
/*********** tilhører: Document-Level:init ***********/

function Time2Num(sFormat,sTime) {
if(sTime == "") return "";
var oTime=util.scand("mm/dd/yyyy "+sFormat, "01/01/1970 "+sTime);
var fTZOffset=oTime.getTimezoneOffset()*1000*60;
var fTime=oTime.valueOf()-fTZOffset;
return Math.round(fTime/1000);
}


//</ACRO_script>
//</Document-Level>

//<AcroForm>
//<ACRO_source>Total:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:Total:Calculate ***********/
var nb=0;
for (var i=0; i<this.numFields; i++) {
if (this.getField(getNthFieldName(i)).name.indexOf("totalTime.")==0) nb++;
}
var sum=0;
for (var i=0; i<nb; i++) {
var time=this.getField("totalTime."+i).value;
var sum=sum+Time2Num("hh:mm",time);
}
var inMinutes=Math.round(sum/60);
var hours=Math.floor(inMinutes/60);
var minutes=((inMinutes/60)-hours)*60;
var min=util.printf("%,302.0f",minutes);
event.target.value=hours+":"+min;

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.0:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.0:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.1:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.1:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.10:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.10:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.11:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.11:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.12:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.12:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.13:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.13:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.14:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.14:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.15:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.15:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.16:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.16:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.17:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.17:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.18:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.18:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.19:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.19:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.2:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.2:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.20:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.20:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.21:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.21:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.22:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.22:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.23:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.23:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.24:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.24:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.25:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.25:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.26:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.26:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.27:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.27:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.28:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.28:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.29:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.29:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.3:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.3:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.30:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.30:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.4:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.4:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.5:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.5:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.6:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.6:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.7:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.7:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.8:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.8:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

//<AcroForm>
//<ACRO_source>totalTime.9:Calculate</ACRO_source>
//<ACRO_script>
/*********** tilhører: AcroForm:totalTime.9:Calculate ***********/
indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

//</ACRO_script>
//</AcroForm>

 

TOPICS
How to , JavaScript

Views

4.8K

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 7 Correct answers

Community Expert , Apr 26, 2021 Apr 26, 2021

Hi,

 

This is off the top of my head but I think you first code can be changed to (haven't had a chance to test it though)

// you should be able to 
var nb = this.getField("totalTime").getArray();
for (var i=0; i<nb.length; i++) {
    var time=nb[i].value;
    var sum=sum+Time2Num("hh:mm",time);
}
var inMinutes=Math.round(sum/60);
var hours=Math.floor(inMinutes/60);
var minutes=((inMinutes/60)-hours)*60;
var min=util.printf("%,302.0f",minutes);
event.target.value=hours+":"+min;

 

That will loop

...

Votes

Translate

Translate
Community Expert , Apr 28, 2021 Apr 28, 2021

Since your "Time2Num" function uses the the base time and TZ offeset, why not use it for your final time formatting? It's much easier than 

 

// Subtract 24 hours from existing result if midnight crossing
if (fDiff<0) fDiff += 24*3600;

var fTZOffset =util.scand("mm/dd/yyyy", "01/01/1970 ").getTimezoneOffset()*1000*60;
var dtDiff = new Date(fDiff+ fTZOffset);
event.value = util.printd("HH:MM",dtDiff);

 

You should setup the timezone offset as a document level variable since it only needs to be

...

Votes

Translate

Translate
Community Expert , May 02, 2021 May 02, 2021

So there are two errors in the code. 

First, the line that gets the break time number has the incorrect format. It should be this.

var fBreak=Time2Num ("hh:mm",sBreak);

 

This should fix your original code. But I like my code better, unfortunately it has a mistake as well.

The difference date needs the inputs to be muliplied by 1000 to get miliseconds. In your code all the time numbers are in seconds. So here is the correct code:

// Subtract 24 hours from existing result if midnight crossing
if 
...

Votes

Translate

Translate
Community Expert , May 03, 2021 May 03, 2021

Place this code in the document script. You'll need to also remove this line of code from the main calculation script. 

var fTZOffset =util.scand("mm/dd/yyyy", "01/01/1970 ").getTimezoneOffset()*1000*60;

 

The other line of code for "fTZOffset" depends on the creation of the oTime variable. So it's not generic and cannot be used in a document script. Make sure you delete it.  You can see there is an error by looking in the console window. You should look in the console any time you write code to

...

Votes

Translate

Translate
Community Expert , May 04, 2021 May 04, 2021

BarleaDC is correctish. 

Where there are real values, the calculation works as expected. Its falling apart where there are non-values. Obviously, the value test code that is supposed to block this in the calculation script it not up to snuff and needs some adjustment.  Do not use that code template you put in the last post. In fact, the code structure you need is already in the calculation script. The solution is much simpler than you think. 

 

It is this line that needs to be adjusted:

if(sStar
...

Votes

Translate

Translate
Community Expert , May 04, 2021 May 04, 2021

You are correct. all of the value need to be compared to "-" for the non-value selection. I swear it was like that in my test code. Don't know why it didn't copy over. But yes, set all the "" to "-".

 

You are over complicating the total calcualtion. The fields are already grouped so you can easily acquire all the fields with the getArray() function. The calculation can also be simplified. 

 

Try this code. This script uses JS features only available in Acrobat DC.

 

var nTotalSeconds = 0;
this.
...

Votes

Translate

Translate
Community Expert , May 05, 2021 May 05, 2021

As I stated previously, the calculation order is incorrect. The total is calculated before the individual times. 

Something else you need to do to this form is change all the dropdowns to "Commit selected items immediately", which you'll find on the options tab of the field properties dialog. 

 

The "oTime" variable is in the "Time2Num()" function. It gets set to null when an invalid date is passed into the function. If you are going to allow the "-" selection, then this is going to happen. So,

...

Votes

Translate

Translate
Explorer ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

The crazy amount of code is of course what's included in the js of my worksheet.

I do experience a lot of lag whenever i update daily work hours, are there any way for me to reduce the lag by programming smarter?

 

Also while i'm at it, sometimes the overall total will show that there is 0.01 hr/min registered in the timesheet, even though there are no recorded hours in the sheet. How can that be, and can it be solved?

Votes

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
Community Expert ,
Apr 23, 2021 Apr 23, 2021

Copy link to clipboard

Copied

Hi,

 

The form is probably slow because any calculate code is run everytime any field changes, it can be a good idea to try and rewrtie your code to use the validation event, as this is only run when the field itself is updated.

 

I am not entirely sure what the code is doing but you are calling "for (var i=0; i<this.numFields; i++) {" and then you only appear to be getting the fields that start with "totalTime." but I think you can call this.getField("totalTime").getArray(), and you will get just the fields that start with that name.

 

(sorry I haven't looked into your problem, will do that now but wanted to get these comments here).

Votes

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 25, 2021 Apr 25, 2021

Copy link to clipboard

Copied

Thanks for your answer BarlaeDC, you've given me a lot to think about/figure out.


The validity event suggestion seems like a logical solution. If i understand it correctly i can validate each calculated total time so that it shouldn't run if certain parameters aren't met? Could a validation event, stop a function if no change is observed?

 

Again, thanks a lot for taking your time to look at my problem.

Votes

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

Hi BarlaeDC

So i've followed your solution regarding the array, and that seems to disable the total calculation, have i done it correctly or am i missing something?

 

//Total Calculate function

var nb=0;
for (var i=0; i<this.getField("totalTime").getArray(); i++) {
if (this.getField(getNthFieldName(i)).name.indexOf("totalTime.")==0) nb++;
}
var sum=0;
for (var i=0; i<nb; i++) {
var time=this.getField("totalTime."+i).value;
var sum=sum+Time2Num("hh:mm",time);
}
var inMinutes=Math.round(sum/60);
var hours=Math.floor(inMinutes/60);
var minutes=((inMinutes/60)-hours)*60;
var min=util.printf("%,302.0f",minutes);
event.target.value=hours+":"+min;

 

This is how a basic calculate function for one row, looks in my form:

 

indice=event.target.name.substr(event.target.name.indexOf(".")+1);
var sStart=this.getField("startingTime."+indice).value;
var sEnd=this.getField("endingTime."+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;
}

Votes

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

Copy link to clipboard

Copied

Hi,

 

This is off the top of my head but I think you first code can be changed to (haven't had a chance to test it though)

// you should be able to 
var nb = this.getField("totalTime").getArray();
for (var i=0; i<nb.length; i++) {
    var time=nb[i].value;
    var sum=sum+Time2Num("hh:mm",time);
}
var inMinutes=Math.round(sum/60);
var hours=Math.floor(inMinutes/60);
var minutes=((inMinutes/60)-hours)*60;
var min=util.printf("%,302.0f",minutes);
event.target.value=hours+":"+min;

 

That will loop all the fields ( I hope)

Votes

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

That's awesome. Works like a charm! I have a question though.

I only implemented your solution in the first "calculation function" which would be the total hours worked in the 1st row. I haven't implemented your solution in the other rows, though it still seems to work there. Is it meant to do that, or is there something happening which i don't understand.

Votes

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

Nvm that reply. I'm dumb, there is of course only one total calculate field where this is applicable so my question makes no sense 😛 .

 

Though it certainly made the pdf 10x faster, it created an other problem as well. Now everytime i put a value/hours into the form, the overall total keeps on multiplying them, instead of adjusting to what amount of hrs there is actually registered in the form. (See pic for reference).

Jakob5FD0_1-1619441053305.png

 

I've also got another problem now. I've figured out how to make a Break/subtract function, though it does create certain problems for me as it sets the total value to 24:00 almost every other time the forms values are adjusted.

 

Here is my totalTime code which now also includes the break code:

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;
}

 

Again thanks a lot for your time 🙂

Votes

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
Community Expert ,
Apr 27, 2021 Apr 27, 2021

Copy link to clipboard

Copied

Hi,

 

I can't see anything obvious in your code, are you able to share the document or a sample document that shows the problem? so that I can see the issue first hand.

Votes

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 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

Yes of course. I'm only a part time employed student so please apologize for my lack of attendance in the forum 🙂

 

I've attached my work-in-progress PDF with the described problems, in this comment.

Votes

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
Community Expert ,
Apr 28, 2021 Apr 28, 2021

Copy link to clipboard

Copied

Since your "Time2Num" function uses the the base time and TZ offeset, why not use it for your final time formatting? It's much easier than 

 

// Subtract 24 hours from existing result if midnight crossing
if (fDiff<0) fDiff += 24*3600;

var fTZOffset =util.scand("mm/dd/yyyy", "01/01/1970 ").getTimezoneOffset()*1000*60;
var dtDiff = new Date(fDiff+ fTZOffset);
event.value = util.printd("HH:MM",dtDiff);

 

You should setup the timezone offset as a document level variable since it only needs to be calculated once. 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
May 01, 2021 May 01, 2021

Copy link to clipboard

Copied

Ahh so now it randomly -24 hrs to the total as it loads the variable twice. Setting one global variable should solve that? 

 

Am I understanding that correctly?

 

 

Votes

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
Community Expert ,
May 02, 2021 May 02, 2021

Copy link to clipboard

Copied

So there are two errors in the code. 

First, the line that gets the break time number has the incorrect format. It should be this.

var fBreak=Time2Num ("hh:mm",sBreak);

 

This should fix your original code. But I like my code better, unfortunately it has a mistake as well.

The difference date needs the inputs to be muliplied by 1000 to get miliseconds. In your code all the time numbers are in seconds. So here is the correct code:

// Subtract 24 hours from existing result if midnight crossing
if (fDiff<0) fDiff += 24*3600;

var fTZOffset =util.scand("mm/dd/yyyy", "01/01/1970 ").getTimezoneOffset()*1000*60;
var dtDiff = new Date((fDiff+ fTZOffset)*1000);
event.value = util.printd("HH:MM",dtDiff);

 

So the global variable has nothing to do with the issue, and there was never any reason to thing it would. The best way to develope/debug JavaScript code is to run it line by line  in the console window.  Then the issues become obvious. It also really helps to develop your progamming skills. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Thanks for your answer, and sorry for dragging this out.
I've corrected the . to : in my calculations. This didn't fix my problems though.

If i understand your solution correctly, i have to take:

var fTZOffset=oTime.getTimezoneOffset()*1000*60;

 Out of the Time2Num function and place it above the function so it can be used as a global variable?

 

Afterwards i've swapped out my code with yours in the calculations, so that they now look like this.

/*********** tilhører: AcroForm:totalTime.9:Calculate ***********/
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;

var fTZOffset =util.scand("mm/dd/yyyy", "01/01/1970 ").getTimezoneOffset()*1000*60;
var dtDiff = new Date((fDiff+ fTZOffset)*1000);
event.value = util.printd("HH:MM",dtDiff);
}

When i do that every total time multiplies by 9 hrs. That is to say 24:00 is instead now 08:59. How can this be?

I've attached my PDF 🙂

 

 

Votes

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
Community Expert ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Place this code in the document script. You'll need to also remove this line of code from the main calculation script. 

var fTZOffset =util.scand("mm/dd/yyyy", "01/01/1970 ").getTimezoneOffset()*1000*60;

 

The other line of code for "fTZOffset" depends on the creation of the oTime variable. So it's not generic and cannot be used in a document script. Make sure you delete it.  You can see there is an error by looking in the console window. You should look in the console any time you write code to see if there are any errors. This information is critical for us to help you debug your code. 

 

Otherwise the code is fine and will work when you fix the "fTZOffset" line in the document script.

 

However, there is one thing you should do to improve the form, which is to move all the code in the "totalTime" calculation into a document level function. Then call this function from the calculation script. This techniqe will save you a lot of time maintaining the form, i.e. you can make updates to the calculation code without having to update all the fields. 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

I've followed your advice and can happily say that you've spared me for at least 600 lines of code. Thank you!
The problem still persist though as you can see the form still says 24:00 even though no value has been filled out in either the beginning, end and break fields.

Jakob5FD0_0-1620117662584.png

I've thought about the possibility of maybe encapsulating the called calculation function, in another function which checks and only runs if values have been entered in relevant fields. Would that work.

So far i've found this solution which i'm not sure would help my case.

var something = (function() {
    var executed = false;
    return function() {
        if (!executed) {
            executed = true;
            // do something
        }
    };
})();

 I've attached the pdf in this message

Votes

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
Community Expert ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Hi,

 

It looks like there is a possibility that in the Time2Num function that oTime is being set to null, which then means everything after it is unpredictable, so it may be better to check that for null, and set it to a meaningful value at that point.

 

It might be related to the above, but when you have a field that is not filled in the value for

sStart is "-"

sEnd is "-'

sBreak is "00:00"

So therefore your code to check if ( sStart == "" ....... ) is not working as expected because the values are not empty as you expect.

 

Hope this helps

 

Votes

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
Community Expert ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

BarleaDC is correctish. 

Where there are real values, the calculation works as expected. Its falling apart where there are non-values. Obviously, the value test code that is supposed to block this in the calculation script it not up to snuff and needs some adjustment.  Do not use that code template you put in the last post. In fact, the code structure you need is already in the calculation script. The solution is much simpler than you think. 

 

It is this line that needs to be adjusted:

if(sStart != "" & sEnd != "" & sBreak != "") {

It contains several functional (not syntax) errors.

First, "&" is a bitwise operator and you need the logical operator "&&"

Next, the biggest error is the non-selected value is "-" not "". 

Then for needness the conditional statements need to be grouped.

 

if((sStart != "-") && (sEnd != "") && (sBreak != "")) {

 

Next, the code does not contain an alternate value for "event.value" all logical paths need to yield a result. 

add this to he bottom of the if statement

 

else
   event.value = "";

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

YOU'RE A GENIUS! Thanks a lot haha!! It works perfectly now! And i even understand why it works! Thanks a thousand times!

I have one wonder question though. Why not fill "-" out in sEnd != "" or sBreak != "" ? i mean the empty values in those are the same as it is in sStart, so shouldnt those be filled in as well?

 

The last problem i'm facing now is my overall totals calculation, which used to work, but have since then been scrambled as you have helped me solve the other problems. Its function is to calculate the overall total of all values in the totalTimes column. This needs to be done in a loop that updates whenever values change for the day.

var nb=0;
for (var i=0; i<this.numFields; i++) {
	if (this.getField(getNthFieldName(i)).name.indexOf("totalTime.")==0) nb++;
}
var sum=0;
for (var i=0; i<nb; i++) {
	var time=this.getField("totalTime."+i).value;
	var sum=sum+Time2Num("hh:mm",time);
}
var inMinutes=Math.round(sum/60);
var hours=Math.floor(inMinutes/60);
var minutes=((inMinutes/60)-hours)*60;
var min=util.printf("%,302.0f",minutes);
event.target.value=hours+":"+min;

 The code looks like this.

 

Jakob5FD0_0-1620146043697.png

 

 

 

Votes

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
Community Expert ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

You are correct. all of the value need to be compared to "-" for the non-value selection. I swear it was like that in my test code. Don't know why it didn't copy over. But yes, set all the "" to "-".

 

You are over complicating the total calcualtion. The fields are already grouped so you can easily acquire all the fields with the getArray() function. The calculation can also be simplified. 

 

Try this code. This script uses JS features only available in Acrobat DC.

 

var nTotalSeconds = 0;
this.getField("totalTime").getArray().forEach(function(a){nTotalSeconds+= Number(Time2Num("hh:mm",a.value))});

var nHours = Math.floor(nTotalSeconds/3600);
var nMinutes = (nTotalSeconds%3600)/60;
event.value = util.printf("%d:%,302.0f",nHours,nMinutes)

 

And finally, the calculation order of your fields is wrong.  Put the Total field at the end.

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

I decided to not include the "-" in break as the total time shouldn't be dependent on a value in that field, if its the case that an employee takes no breaks. I've also moved the overall totalTime to the bottom of the script, as you suggested.

 

Thanks for the code snippet. It updates now, though not to the correct values.

Jakob5FD0_0-1620199148024.png

The console log describes

TypeError: oTime is null
7:Document-Level:init

&

TypeError: oTime is null
6:Document-Level:init

I'm not sure i understand why that is, though it have appeared over a long time now.

Votes

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
Community Expert ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

As I stated previously, the calculation order is incorrect. The total is calculated before the individual times. 

Something else you need to do to this form is change all the dropdowns to "Commit selected items immediately", which you'll find on the options tab of the field properties dialog. 

 

The "oTime" variable is in the "Time2Num()" function. It gets set to null when an invalid date is passed into the function. If you are going to allow the "-" selection, then this is going to happen. So, you need to add code to test for this null value and return 0 as the time value. 

 

like this.

 

if(oTime == null)

  return 0;

 

Another problem with the calculation is that it allows the break time to be greater than the work time. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

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 ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

LATEST

Thanks a ton! Now everything works as it should. I made the mistake of believing that the calculation order could be edited in the js like you would normally do in programming. I figured it out though. For future readers of this long, loooong forum post: You have to enter "prepare form -> More -> Set field calculation order".

 

Again thanks a ton Thom and BarlaeDC! You've really shown me the way, and i've learned a ton!

Votes

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