Skip to main content
Participating Frequently
May 22, 2025
Question

Calculating time for nightshift timesheet

  • May 22, 2025
  • 1 reply
  • 365 views

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

 

1 reply

try67
Community Expert
Community Expert
May 22, 2025

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

Participating Frequently
May 22, 2025

Hi, thank you so much for this, I have updated it but unfortunately I am still getting the wrong answer:

 

try67
Community Expert
Community Expert
May 22, 2025

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.