Skip to main content
Participating Frequently
April 20, 2023
Answered

JavaScript to calculate hours between a start time/date and end time/date

  • April 20, 2023
  • 3 replies
  • 30557 views

I've looked all over in the community forums and tried a few things that people suggested 

 

I am having trouble finding the right script to calculate hours between a start date/time and the end date/time. 

I am not sure if the problem lies with the input fields or the spot where I am inputting the script

 

I never have to work with scripts - just need some help this one time. 

Correct answer Nesa Nurani

Hi - looking for a follow-up.  Please see attached.  Between the hours of 0700 and 1700 today, the script calculates 90.98 hours


You have wrong fields in 'datestarted' and 'datefinished' variables.

Try this:

 

var timefinished = this.getField("TIME.FINISHED").valueAsString;
var timestarted = this.getField("TIME.STARTED").valueAsString;

var datefinished = this.getField("EndDate").valueAsString;
var datestarted = this.getField("StartDate").valueAsString;

if (datefinished && datestarted && timefinished && timestarted) {
	var datetimefinished = util.scand("yyyy-mm-dd HH:MM", datefinished + " " + timefinished);
	var datetimestarted = util.scand("yyyy-mm-dd HH:MM", datestarted + " " + timestarted);

event.value = (datetimefinished - datetimestarted)/(60 * 60 * 1000);
} else event.value = "";

 

3 replies

Participating Frequently
March 6, 2025



New Participant
June 1, 2024

Calculating the hours between a start date/time and an end date/time can indeed be tricky if you're not familiar with scripting. Here's a simple JavaScript function you can use:

function calculateHours(startDate, endDate) {
const start = new Date(startDate);
const end = new Date(endDate);
const milliseconds = Math.abs(end - start);
const hours = milliseconds / (1000 * 60 * 60);
return hours;
}

// Example usage
const startDateTime = '2024-06-01T08:00:00'; // Format: YYYY-MM-DDTHH:MM:SS
const endDateTime = '2024-06-01T12:30:00'; // Format: YYYY-MM-DDTHH:MM:SS
const hoursDifference = calculateHours(startDateTime, endDateTime);
console.log('Hours between start and end:', hoursDifference);

This function takes the start date/time and end date/time as inputs, converts them to Date objects, calculates the difference in milliseconds, and then converts it to hours. You can use this function by passing your start and end date/time values to it.

If you're seeking more information or resources regarding date and time calculations, feel free to reach out!

try67
Braniac
April 20, 2023

The first thing to do is to remove the Number format setting you used for the Total Hours field, since the result it's supposed to show is not a number, but a string.

Then you need to take into account the date fields you've created, and add them to your code (instead of 01/01/1970...).

Also, you need to take into account what happens if any of the four input fields are empty. You'd probably don't want to display anything in that situation.

After you've taken care of all of that see what output your code produces, and if it's incorrect post back here.

Participating Frequently
April 21, 2023

Hi thanks - I think I made those changes as you suggested. I am getting something to show in the Total Hours field but not what I intended

try67
Braniac
April 21, 2023

You only applied the first change I mentioned.