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

Calculating Total of total overtime & calculating time after 12 midnight

Community Beginner ,
Oct 17, 2024 Oct 17, 2024

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

TOPICS
Create PDFs , JavaScript , Modern Acrobat , PDF forms
4.4K
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 Expert ,
Oct 17, 2024 Oct 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 ;
}

 

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 Expert ,
Oct 17, 2024 Oct 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 ;
}

 

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 ,
Oct 20, 2024 Oct 20, 2024

Thank you very much. It worked.

 

But how to calculate all totals?

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 ,
Oct 21, 2024 Oct 21, 2024

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

 

Thank you very much

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 ,
Oct 23, 2024 Oct 23, 2024

I have an issue with calculation.

It doesnt count "0" (from 00:00 till 01:05 = total 1.5). Attached

How to fix this?

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 Expert ,
Oct 23, 2024 Oct 23, 2024

I'm confused.  Your screenshot shows that it does.

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 ,
Oct 24, 2024 Oct 24, 2024

The first row: from 00:00 till 01:52 = 1.52 total hours

the second row: from 00:00 till 01:05 = 1.5 total hours (shouldn't be 1.05 is the total?)

I used your code for total

 

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

 

Thank you and sorry for bad explanation

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 Expert ,
Oct 24, 2024 Oct 24, 2024

You must format the minutes with 2 digits.

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 ,
Oct 24, 2024 Oct 24, 2024

It shows 1.50, it should be 1.05

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 Expert ,
Oct 24, 2024 Oct 24, 2024

Change this line (near the end):

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

to this:

event.value = hours + "." + util.printf("%02d", minutes) ;

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 25, 2025 Apr 25, 2025
LATEST

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.

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