Skip to main content
Participant
August 25, 2022
Answered

Extraction of time difference

  • August 25, 2022
  • 3 replies
  • 1287 views

I want to take out the difference between the two hours, but it was 9 hours more. What is wrong?

 

 

var endTime = this.getField("Text4").valueAsString;
var startTime = this.getField("Text2").valueAsString;
if (endTime=="") event.value = "";
else {
var d = util.scand("mm/dd/yyyy HH:MM", "01/01/1970 " + endTime);
var s = util.scand("mm/dd/yyyy HH:MM", "01/01/1970 " + startTime);
d.setTime(d.getTime()-s.getTime());
event.value = util.printd("HH:MM", d);
}

This topic has been closed for replies.
Correct answer try67

var diffInHours = (d.getTime()-s.getTime())/3600000;
event.value = diffInHours;

 

This will result in a decimal values, though (1.5, for example), to convert it to a time-format (1:30) would require some additional code.

3 replies

Participant
June 1, 2024

It seems that the issue might lie in how you're handling the time difference calculation. Make sure that the input format for both the start and end times is consistent and correctly formatted.
Also, double-check the logic for subtracting the start time from the end time to calculate the difference.

If you're encountering a discrepancy of 9 hours, it could be due to time zone differences or incorrect time formatting. Consider using a reliable date and time calculator to accurately compute the time difference, which can help identify any errors in your script.

try67
Community Expert
Community Expert
August 25, 2022

This is not a good way of calculating a time difference. You should take the result of subtracting the values returned by getTime() on both objects and then convert it yourself into hours, by dividing it by 3600000.

Participant
August 25, 2022

Could you present the program?
I am trying programming for the first time and don't know what to fix...

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
August 26, 2022

var diffInHours = (d.getTime()-s.getTime())/3600000;
event.value = diffInHours;

 

This will result in a decimal values, though (1.5, for example), to convert it to a time-format (1:30) would require some additional code.

Bernd Alheit
Community Expert
Community Expert
August 25, 2022

What values does you use?