Skip to main content
Known Participant
October 17, 2024
Answered

Calculating Total of total overtime & calculating time after 12 midnight

  • October 17, 2024
  • 2 replies
  • 4036 views

I used below code from other post to calculate total time (custom calculate script):

 

if ((this.getField("To1").value.length == 0) || (this.getField("From1").value.length == 0)) {

event.value = " 0 Hours 0 Minutes";

}

else{

 

var timefinished = this.getField("To1").value;

var timestarted = this.getField("From1").

 

value; var datetimefinished = new Date('1970/01/01' + " " + timefinished);

var datetimestarted = new Date('1970/01/01' + " " + timestarted);

 

var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;

 

var hours = Math.floor(difflnMilliSeconds / 3600) % 24;

difflnMilliSeconds -= hours *3600;

 

var minutes = Math.floor(difflnMilliSeconds / 60) % 60;

difflnMilliSeconds -= minutes * 60;

 

event.value = hours + "." + minutes ;

}

 

I am having trouble finding the right script to calculate all totals and how to calculate overtime after 12 am (for Example: From 23:00 till 01:00). 

Correct answer PDF Automation Station

Try this (for Total5) and adjust the other field names for the other fields:

 

if ((this.getField("To5").value.length == 0) || (this.getField("From5").value.length == 0)) {
event.value = " 0 Hours 0 Minutes";
}
else{

var timefinished = this.getField("To5").value;
var timestarted = this.getField("From5").value;

var datetimefinished = new Date('1970/01/01' + " " + timefinished);
var datetimestarted = new Date('1970/01/01' + " " + timestarted);

if(datetimefinished<datetimestarted)
{
var date=new Date(datetimefinished)
date.setDate(date.getDate()+1);
datetimefinished=date;
}

var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;

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

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

event.value = hours + "." + minutes ;
}

 

2 replies

Participating Frequently
April 25, 2025

You're close! The issue with calculating time that passes over midnight (e.g., 23:00 to 01:00) is that JavaScript thinks 01:00 is earlier than 23:00 on the same day. To fix this, you need to add a day if the end time is less than the start time.

Here’s a corrected and improved version of your script that:

Calculates total time

Handles overnight shifts (e.g., 23:00 to 01:00)

Returns result in hours.minutes format

if ((this.getField("To1").value.length == 0) || (this.getField("From1").value.length == 0)) {
event.value = "0.0";
} else {
var start = this.getField("From1").value;
var end = this.getField("To1").value;

var startTime = new Date("1970/01/01 " + start);
var endTime = new Date("1970/01/01 " + end);

// If time crosses midnight, add 1 day to endTime
if (endTime < startTime) {
endTime.setDate(endTime.getDate() + 1);
}

var diff = (endTime - startTime) / 1000; // seconds
var hours = Math.floor(diff / 3600);
var minutes = Math.floor((diff % 3600) / 60);

event.value = hours + "." + (minutes < 10 ? "0" + minutes : minutes);
}
For Total Overtime
If you have multiple time fields and want to calculate a total, you’ll need a separate field that sums all hour.minutes values, converting them properly to minutes first. Let me know if you need that part too.

PDF Automation Station
Community Expert
Community Expert
October 17, 2024

Try this (for Total5) and adjust the other field names for the other fields:

 

if ((this.getField("To5").value.length == 0) || (this.getField("From5").value.length == 0)) {
event.value = " 0 Hours 0 Minutes";
}
else{

var timefinished = this.getField("To5").value;
var timestarted = this.getField("From5").value;

var datetimefinished = new Date('1970/01/01' + " " + timefinished);
var datetimestarted = new Date('1970/01/01' + " " + timestarted);

if(datetimefinished<datetimestarted)
{
var date=new Date(datetimefinished)
date.setDate(date.getDate()+1);
datetimefinished=date;
}

var difflnMilliSeconds = Math.abs(datetimefinished - datetimestarted)/1000;

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

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

event.value = hours + "." + minutes ;
}

 

Known Participant
October 20, 2024

Thank you very much. It worked.

 

But how to calculate all totals?

Known Participant
October 21, 2024

Opps.. sorry. Sum(+) has done the job. 

 

Thank you very much