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

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

Explorer ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

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.

TOPICS
How to , JavaScript , PDF forms

Views

1.0K

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

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

Votes

Translate

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

Copy link to clipboard

Copied

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). 

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

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.

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 you answers. As i understand the timezone code is what scrambles my calculations?

I've read your suggestions, and they sound very logical, i'm just not sure how to code them. I'm still a beginner with js. time calculation and acrobat, so if you could give me an example in code or redirect me to an example then i would appreciate that 🙂

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

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

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 a lot! Works like a charm, and much easier to understand and read than whatever solution i had come up with. I have one question though, which might be a dumb one as i'm not fond of math, but why divide by 3600.

 

 

Sometimes though, it still subtracts -1 to the total time so it sometimes says 0:00 and then others times 23:59. I read somewhere that this kind of subtraction happens as a result of the js calculation and that there is nothing to do about it.

 

Is that true and if so, is it then possible to set up parameters so that certain fields aren't calculated unless there is value present in the row?

 

I've attached an image of the form so that you might better understand what i mean 🙂

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

3600 is the number of seconds in an hour, and Time2Num returns the value in seconds.

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

Ahh of course. Thank you 🙂

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

I just looked at your form, and it seems to report times that are one second after whaterver was selected. I don't have the time right now to dig into this, but there must be something wrong with how you calculate the in, out and break times. 

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

LATEST

I think you're right, but where in the code the bug is, is a complete mystery to me. I'm pretty sure that it has nothing to do with the hours crossing 24 hours, as it seems to occur randomly. There is also no feedback in the console...

 

Thanks a lot for the help so far! I wish you an awesome evening 🙂

 

 

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