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

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

80

Likes

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.

Likes

Translate

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

Copy link to clipboard

Copied

What values does you use?

Likes

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.

Likes

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

Likes

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.

Likes

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

LATEST

Thanks!

Likes

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