Skip to main content
Known Participant
May 3, 2021
Answered

JS [Timesheet] - Day total time is reset to 08:59 hours total time, instead of 00:00

  • May 3, 2021
  • 9 replies
  • 1947 views

Hi
My timesheet currently displays it's neutral/null titak time as 08:59.
What am i doing wrong in my calculations?

 

// Example of my Time2Num function which calculates my time format.
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);
}

//Example of a totalTime table that currently returns 09:00 instead of 00:00
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);
}

 

I've attached the pdf if you want to look at it yourself.

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Karl Heinz Kremer

Once you've calcuated fDiff, this should work:

 

    var hours = Math.floor(fDiff / 3600);
    var minutes = Math.floor(fDiff % 3600 / 60);
    var seconds = Math.floor(fDiff % 3600 % 60);

    while ((String(minutes).length) < 2)
        minutes = "0" + String(minutes);
    
    event.value = String(hours) + ":" + String(minutes);

9 replies

Karl Heinz  Kremer
Community Expert
Community Expert
May 3, 2021

When you calcuate the difference between the end and start times, taking into account the break time, you have a time difference that is independent of the local timezone, so there is no need to take that timezone into account (which you are doing via the fTZOffset). From what I can tell, you have the time difference in seconds, I would just convert that directly to hours and minutes and then assemble the displayed string by just taking the hours, adding the delimiter ":" and then add the minutes (taking leading zeros into account). 

try67
Community Expert
Community Expert
May 3, 2021

And you also shouldn't convert it to a Time string (using printd), as the result is not an hour but an amount of time.