Copy link to clipboard
Copied
Hi all, I received help previously for a script to calculate duration between 2 times, however it doesn't work if we have nightshift times (i.e. from 10pm to 5am). If anyone could help to adjust it for nightshift that would be appreciated. Here is the current script and the form:
if ((this.getField("End TimeRow1").value.length == 0) || (this.getField("Start TimeRow1").value.length == 0)) {
event.value = " 0 Hours 0 Minutes";
}
else{
var timefinished = this.getField("End TimeRow1").value;
var timestarted = this.getField("Start TimeRow1").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/60) ;
}
Copy link to clipboard
Copied
Change these two lines:
var datetimefinished = new Date('1970/01/01' + " " + timefinished);
var datetimestarted = new Date('1970/01/01' + " " + timestarted);
To:
var datetimefinished = util.scand("yyyy/mm/dd hh:MMtt", "1970/01/01 " + timefinished);
var datetimestarted = util.scand("yyyy/mm/dd hh:MMtt", "1970/01/01 " + timestarted);
Copy link to clipboard
Copied
Hi, thank you so much for this, I have updated it but unfortunately I am still getting the wrong answer:
Copy link to clipboard
Copied
The problem is they don't fall within the same calendar day.
One way to solve it is to add 24 hours to the result if it's negative.
Copy link to clipboard
Copied
Or include the date as well as time.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now