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

Extraction of time difference

Community Beginner ,
Aug 25, 2022 Aug 25, 2022

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

TOPICS
Create PDFs , Edit and convert PDFs , JavaScript

Views

760

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Aug 26, 2022 Aug 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.

Votes

Translate

Translate
Community Expert ,
Aug 25, 2022 Aug 25, 2022

Copy link to clipboard

Copied

What values does you use?

Votes

Translate

Translate

Report

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 ,
Aug 25, 2022 Aug 25, 2022

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.

Votes

Translate

Translate

Report

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 Beginner ,
Aug 25, 2022 Aug 25, 2022

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...

Votes

Translate

Translate

Report

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 ,
Aug 26, 2022 Aug 26, 2022

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.

Votes

Translate

Translate

Report

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 Beginner ,
Aug 29, 2022 Aug 29, 2022

Copy link to clipboard

Copied

Thanks!

Votes

Translate

Translate

Report

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 ,
Jun 01, 2024 Jun 01, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

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