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

Time Sheet Calculations

Community Beginner ,
Apr 26, 2023 Apr 26, 2023

I've created a time sheet for work, however, I can't seem to figure out how to add the morning time with the afternoon time. I've looked through many of the examples and discussions through the forum, but cant seem to find exactly what I'm looking for. I'm just trying to figure out the first row. Please help

 

Here is my code:

if (((this.getField("MorningR1Out").value.length == 0) || (this.getField("MorningR1In").value.length == 0)) && ((this.getField("AfternoonR1Out").value.length == 0) || (this.getField("AfternoonR1In").value.length == 0)))

 

{ event.value = " 0 Hours 0 Minutes"; }

 

else

{ var mortimefinishedR1 = this.getField("MorningR1Out").value;

var morningtimestartedR1 = this.getField("MorningR1In").value;

var afttimefinishedR1 = this.getField("AfternoonR1Out").value;

var afttimestartedR1 = this.getField("AfternoonR1In").value;

var mordatetimefinishedR1 = new Date('1970/01/01' + " " + morntimefinishedR1);

var mordatetimestartedR1 = new Date('1970/01/01' + " " + mortimestartedR1);

var aftdatetimefinishedR1 = new Date('1970/01/01' + " " + afttimefinishedR1);

var aftdatetimestartedR1 = new Date('1970/01/01' + " " + afttimestartedR1);

var mordifflnMilliSecondsR1 = Math.abs(mordatetimefinishedR1 - mordatetimestartedR1)/1000;

var aftdifflnMilliSecondsR1 = Math.abs(aftdatetimefinishedR1 - aftdatetimestartedR1)/1000; // calculate morning hours

var morhoursR1 = Math.floor(mordifflnMilliSecondsR1 / 3600) % 24; mordifflnMilliSecondsR1 -= morhoursR1 *3600;

 

// calculate afternoon hours

var afthoursR1 = Math.floor(aftdifflnMilliSecondsR1 / 3600) % 24; aftdifflnMilliSecondsR1 -= afthoursR1 *3600;

 

// calculate morning minutes

var morminutesR1 = Math.floor(mordifflnMilliSecondsR1 / 60) % 60; mordifflnMilliSecondsR1 -= morminutesR1 * 60;

 

// calculate afternoon minutes

var aftminutesR1 = Math.floor(mordifflnMilliSecondsR1 / 60) % 60; aftdifflnMilliSecondsR1 -= aftminutesR1 * 60;

 

//morning var

var MorTimeR1 = morhoursR1 + morminutesR1;

 

//afternoon var

var AftTimeR1 = afthoursR1 + aftminutesR1;

 

// set field value to the difference

event.value = MorTimeR1 + AftTimeR1; }

 

TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF forms
1.5K
Translate
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
1 ACCEPTED SOLUTION
Community Beginner ,
May 04, 2023 May 04, 2023
LATEST

I figured it out, i made some changes to the labels of each cell but I'm sure you'll get it. This is working for me now.

if (((this.getField("MorOut.1").value.length == 0) || (this.getField("MorIn.1").value.length == 0)) 
&&
((this.getField("AftOut.1").value.length == 0) || (this.getField("AftIn.1").value.length == 0))) 
{
event.value = "0";
}
else{


var mor1timefinished = this.getField("MorOut.1").value;
var mor1timestarted = this.getField("MorIn.1").value;
var aft1timefinished = this.getField("AftOut.1").value;
var aft1timestarted = this.getField("AftIn.1").value;

var mor1datetimefinished = new Date('1970/01/01' + " " + mor1timefinished);
var mor1datetimestarted = new Date('1970/01/01' + " " + mor1timestarted);
var aft1datetimefinished = new Date('1970/01/01' + " " + aft1timefinished);
var aft1datetimestarted = new Date('1970/01/01' + " " + aft1timestarted);

var mor1difflnMilliSeconds = Math.abs(mor1datetimefinished - mor1datetimestarted)/1000;
var aft1difflnMilliSeconds = Math.abs(aft1datetimefinished - aft1datetimestarted)/1000;

//calculate total milliseconds
var total1milliseconds = mor1difflnMilliSeconds + aft1difflnMilliSeconds;

// calculate total hours
var t1hours = Math.floor(total1milliseconds / 3600) % 24;
total1milliseconds -= t1hours *3600;

// calculate total minutes
var t1minutes = Math.floor(total1milliseconds / 60) % 60;
total1milliseconds -= t1minutes * 60;

// set field value to the difference
event.value = t1hours + "." + t1minutes + "";
}

 

View solution in original post

Translate
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 Beginner ,
Apr 26, 2023 Apr 26, 2023

Updated code

if (((this.getField("MorningR1Out").value.length == 0) || (this.getField("MorningR1In").value.length == 0)) 
&&
((this.getField("AfternoonR1Out").value.length == 0) || (this.getField("AfternoonR1In").value.length == 0))) 
{
event.value = " 0 Hours 0 Minutes";
}
else{

var mortimefinishedR1 = this.getField("MorningR1Out").value;
var morningtimestartedR1 = this.getField("MorningR1In").value;
var afttimefinishedR1 = this.getField("AfternoonR1Out").value;
var afttimestartedR1 = this.getField("AfternoonR1In").value;
var mordatetimefinishedR1 = new Date('1970/01/01' + " " + morntimefinishedR1);
var mordatetimestartedR1 = new Date('1970/01/01' + " " + mortimestartedR1);
var aftdatetimefinishedR1 = new Date('1970/01/01' + " " + afttimefinishedR1);
var aftdatetimestartedR1 = new Date('1970/01/01' + " " + afttimestartedR1);

var mordifflnMilliSecondsR1 = Math.abs(mordatetimefinishedR1 - mordatetimestartedR1)/1000;
var aftdifflnMilliSecondsR1 = Math.abs(aftdatetimefinishedR1 - aftdatetimestartedR1)/1000;

// calculate Milliseconds
var difflnMilliSecondsR1 = Math.abs(mordifflnMilliSecondsR1 + aftdifflnMilliSecondsR1);

// calculate hours
var hoursR1 = Math.floor(difflnMilliSecondsR1 / 3600) % 24;
difflnMilliSeconds -= hoursR1 *3600;

// calculate minutes
var minutesR1 = Math.floor(difflnMilliSecondsR1 / 60) % 60;
difflnMilliSeconds -= minutesR1 * 60;

// set field value to the difference
event.value = hoursR1 + ":" + minutesR1;
}

 

Translate
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 Beginner ,
May 04, 2023 May 04, 2023
LATEST

I figured it out, i made some changes to the labels of each cell but I'm sure you'll get it. This is working for me now.

if (((this.getField("MorOut.1").value.length == 0) || (this.getField("MorIn.1").value.length == 0)) 
&&
((this.getField("AftOut.1").value.length == 0) || (this.getField("AftIn.1").value.length == 0))) 
{
event.value = "0";
}
else{


var mor1timefinished = this.getField("MorOut.1").value;
var mor1timestarted = this.getField("MorIn.1").value;
var aft1timefinished = this.getField("AftOut.1").value;
var aft1timestarted = this.getField("AftIn.1").value;

var mor1datetimefinished = new Date('1970/01/01' + " " + mor1timefinished);
var mor1datetimestarted = new Date('1970/01/01' + " " + mor1timestarted);
var aft1datetimefinished = new Date('1970/01/01' + " " + aft1timefinished);
var aft1datetimestarted = new Date('1970/01/01' + " " + aft1timestarted);

var mor1difflnMilliSeconds = Math.abs(mor1datetimefinished - mor1datetimestarted)/1000;
var aft1difflnMilliSeconds = Math.abs(aft1datetimefinished - aft1datetimestarted)/1000;

//calculate total milliseconds
var total1milliseconds = mor1difflnMilliSeconds + aft1difflnMilliSeconds;

// calculate total hours
var t1hours = Math.floor(total1milliseconds / 3600) % 24;
total1milliseconds -= t1hours *3600;

// calculate total minutes
var t1minutes = Math.floor(total1milliseconds / 60) % 60;
total1milliseconds -= t1minutes * 60;

// set field value to the difference
event.value = t1hours + "." + t1minutes + "";
}

 

Translate
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