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

Calculating time for nightshift timesheet

New Here ,
May 22, 2025 May 22, 2025

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

Carly35100614pxwk_0-1747918030243.png

 

TOPICS
JavaScript
157
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 ,
May 22, 2025 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);

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
New Here ,
May 22, 2025 May 22, 2025

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

Carly35100614pxwk_0-1747918849966.png

 

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 ,
May 22, 2025 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.

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 ,
May 22, 2025 May 22, 2025
LATEST

Or include the date as well as time. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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