Copy link to clipboard
Copied
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);
}
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.
Copy link to clipboard
Copied
What values does you use?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Could you present the program?
I am trying programming for the first time and don't know what to fix...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks!
Copy link to clipboard
Copied
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.