Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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 ;
}
Copy link to clipboard
Copied
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 ;
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Opps.. sorry. Sum(+) has done the job.
Thank you very much
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I'm confused. Your screenshot shows that it does.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
You must format the minutes with 2 digits.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Change this line (near the end):
event.value = hours + "." + minutes ;
to this:
event.value = hours + "." + util.printf("%02d", minutes) ;
Copy link to clipboard
Copied
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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more