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

Time Calculation with break (Day and Night Shift) in 24hr format

New Here ,
May 15, 2024 May 15, 2024

Copy link to clipboard

Copied

I have a timesheet where I need to calculate times for day and night shift workers (with breaks) in 24hr time.

 

I can get the day shift hours to calculate fine however the I cannot get the night shift hours to calculate correctly.  

 

Below is a screen shot of the fields and the field names along with the script I am currently using on the top line (I have used the same script on the second line but changed the field names to match i.e 8.1.0 instead of 8.0.0).

 

Adobe SC.png

var start = this.getField("8.0.0").value;
var finish = this.getField("11.0.0").value;
if (start=="" || finish=="") event.value = "";
else {
var lunchout = this.getField("9.0.0").value;
var lunchin = this.getField("10.0.0").value;
var startArr = start.split(":");
var finishArr = finish.split(":");
var hourWDiff = Math.abs(finishArr[0] - startArr[0]);
var minWDiff = (Math.abs(finishArr[1] - startArr[1])/60*100);
var hourLDiff = 0;
var minLDiff = 0;
if (lunchout!="" && lunchin!="") {
var lunchoutArr = lunchout.split(":");
var lunchinArr = lunchin.split(":");
hourLDiff = Math.abs(lunchoutArr[0] - lunchinArr[0]);
minLDiff = (Math.abs(lunchoutArr[1] - lunchinArr[1])/60*100);
}
var totalHrs = hourWDiff + (minWDiff/100) - (hourLDiff + (minLDiff/100));
event.value = totalHrs;
}

TOPICS
How to , JavaScript , PDF forms

Views

39

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 16, 2024 May 16, 2024

Try this:

var start = this.getField("8.0.0").value;
var finish = this.getField("11.0.0").value;

if (start == "" || finish == "") {
 event.value = "";} 
else {
 var lunchout = this.getField("9.0.0").value;
 var lunchin = this.getField("10.0.0").value;
 var startArr = start.split(":");
 var finishArr = finish.split(":");
 var startHour = parseInt(startArr[0]);
 var finishHour = parseInt(finishArr[0]);
    
 if (finishHour < startHour) {
  finishHour += 24;}
    
 var hourWDiff = finishHour - star
...

Votes

Translate

Translate
Community Expert ,
May 16, 2024 May 16, 2024

Copy link to clipboard

Copied

Try this:

var start = this.getField("8.0.0").value;
var finish = this.getField("11.0.0").value;

if (start == "" || finish == "") {
 event.value = "";} 
else {
 var lunchout = this.getField("9.0.0").value;
 var lunchin = this.getField("10.0.0").value;
 var startArr = start.split(":");
 var finishArr = finish.split(":");
 var startHour = parseInt(startArr[0]);
 var finishHour = parseInt(finishArr[0]);
    
 if (finishHour < startHour) {
  finishHour += 24;}
    
 var hourWDiff = finishHour - startHour;
 var minWDiff = (Math.abs(finishArr[1] - startArr[1]) / 60) * 100;
 var hourLDiff = 0;
 var minLDiff = 0;
    
 if (lunchout != "" && lunchin != "") {
  var lunchoutArr = lunchout.split(":");
  var lunchinArr = lunchin.split(":");
  hourLDiff = Math.abs(lunchoutArr[0] - lunchinArr[0]);
  minLDiff = (Math.abs(lunchoutArr[1] - lunchinArr[1]) / 60) * 100;}
    
 var totalHrs = hourWDiff + (minWDiff / 100) - (hourLDiff + (minLDiff / 100));
 event.value = totalHrs;}

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
New Here ,
May 16, 2024 May 16, 2024

Copy link to clipboard

Copied

LATEST

Thank you so much Nesa, works perfectly 😁

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