Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

- Home
- Acrobat SDK
- Discussions
- 24 hr time format is not giving correct values for...

- 24 hr time format is not giving correct values for...

24 hr time format is not giving correct values for the time difference calculation

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/td-p/11265370
Jul 05, 2020
Jul 05, 2020

Copy link to clipboard

Copied

I have the following code for my pdf time sheet Time In , Time Out in 24 hr time format. When I use 8:30 AM -10:00 AM it give correct value as 1.5. But when I use 11:30 am - 12:15 AM I am expecting 0.75 but I am NOT getting that value. Please advise and help me out here.

```
var start1 = this.getField("Recall1_Wk1_Thurs_TimeIn").valueAsString;
var finish1 = this.getField("Recall1_Wk1_Thurs_TimeOut").valueAsString;
var start2 = this.getField("Recall2_Wk1_Thurs_TimeIn").valueAsString;
var finish2 = this.getField("Recall2_Wk1_Thurs_TimeOut").valueAsString;
var totalTime = 0;
if (start1!="" && finish1!="") {
var startArr = start1.split(":");
var finishArr = finish1.split(":");
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*60);
totalTime+=(hourDiff*60)+minDiff;
}
if (start2!="" && finish2!="") {
var startArr = start2.split(":");
var finishArr = finish2.split(":");
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*60);
totalTime+=(hourDiff*60)+minDiff;
}
var totalTimeHours = Math.floor(totalTime/60);
var totalTimeMinutes = (totalTime-(totalTimeHours*60))/60;
var output = totalTimeHours+totalTimeMinutes;
event.value = output;
```

TOPICS

Acrobat SDK and JavaScript

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

10
Replies
10

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11265550#M79608
Jul 05, 2020
Jul 05, 2020

Copy link to clipboard

Copied

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11265801#M79609
Jul 05, 2020
Jul 05, 2020

Copy link to clipboard

Copied

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

rabbani she
AUTHOR

Community Beginner
,

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11268611#M79610
Jul 06, 2020
Jul 06, 2020

Copy link to clipboard

Copied

Community guidelines

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11268642#M79611
Jul 06, 2020
Jul 06, 2020

Copy link to clipboard

Copied

No, try 8:30 and 10:15

Community guidelines

rabbani she
AUTHOR

Community Beginner
,

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11268763#M79612
Jul 06, 2020
Jul 06, 2020

Copy link to clipboard

Copied

Community guidelines

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11268811#M79613
Jul 07, 2020
Jul 07, 2020

Copy link to clipboard

Copied

Community guidelines

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11268816#M79614
Jul 07, 2020
Jul 07, 2020

Copy link to clipboard

Copied

How does you got the wrong script?

Community guidelines

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11270741#M79615
Jul 07, 2020
Jul 07, 2020

Copy link to clipboard

Copied

What Bernd meant earlier is that your script is basically missing an important part of this calculation.

The first thing I noticed is the same observation as Gkaiseril.

You cannot use AM/PM format if you're using military time format, that is a 24 hour period.

In AM/PM format you deal with hours 1 through 12 and the AM or PM indicator lets you know if it is morning or evening times. In military time format you deal with 24 hour period. Formats are not expressed as 9:00 for example, It has the extended "0" for hours and minutes. Morning begins at 00:00 while Midnight end at 24:00 not 12:00 for example.

So, for example, you will run into conflict with your script because 12:30 (at noon) - 21:05 (9:05 PM) is not greater than the end time.

To do it correctly it will have to be expressed like this:

If starting minutes are greater than the minutes of ending time just substract hours and add the minutes normally (a resulting negative value of the hours shouldn't matter to us, but will will matter in you javascript equation(s) though. So pay attention to those negative totals.

If starting minutes are not greater than the minutes of the ending time, then follow the rule of thumb:

Substract one hour from the ending time hours.

Add 60 minutes to the starting time minutes, then substract the total hours and minutes

That would be the toal in hours and minutes between the two times.

See more here how to do it manually: How To Add and Subtract Time in Hours and Minutes

Your current script not only disregards what happens when the starting time is less than the ending time, but also, disregards what happen in the event that would normally take place at noon (12:00) versus midnight (24:00).

And since you've splitted the resulting hours and minutes as separate strings Bernd pointed out that your script is incomplete, because is missing a basic time arithmetic formula.

When the time is expressed in a numerical manner to substract hour and minutes between two given times, if the hour and minutes of the starting time are less than the hours and minutes of the ending time, you need to use this equation: https://www.aaamath.com/B/meatsbhm.htm

Look in the example in that website. It says:

**What is 4 hours 11 minutes minus 2 hours 47 minutes.**

**The original number of minutes (11) is less than the 47 minutes being subtracted so:**

**Subtract 1 from the original number of hours. (4 hours - 1 hour = 3 hours)**

**Increase the number of minutes by 60. (11 minutes + 60 minutes = 71 minutes)**

**Subtract the minutes. (71 minutes - 47 minutes = 24 minutes)**

**Subtract the hours. (3 hours - 2 hours = 1 hour)**

**Answer: 1 hour 24 minutes**

Using the arithmetic rule posted above, the part you should be looking to fix in your script is here:

```
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*60);
```

You may need to also implement a conditional statement, like Bernd spotted. See this this other part in your script:

`if (start1!="" && finish1!="") {`

It should provide a condition to handle what happens if the starting time is not greater than the ending time, because if it's not, then you have to consider the rule of thumb of substracting 1 from the total hours of the ending time, then add 60 to the total number of minutes of the starting time. Only then you'll be able to totalize correctly. So consider something like:

```
// declare all your variables first
if ( (startArr !="" && finishArr !="") {
// state a condition to handle what happens if the starting time is less than the ending time
if (start1.length < finish1.length) {
// then put your code here
} else {
// do something else or declare no result to display if the conditions above are not met.
//For example, if starting time is greater than ending time then do
if (start1.length > finish1.length) {
//and put the code here to handle that condition
}
}
}
```

Community guidelines

/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11270794#M79616
Jul 07, 2020
Jul 07, 2020

Copy link to clipboard

Copied

Community guidelines

Community Expert
,

LATEST
/t5/acrobat-sdk-discussions/24-hr-time-format-is-not-giving-correct-values-for-the-time-difference-calculation/m-p/11272734#M79617
Jul 08, 2020
Jul 08, 2020

Copy link to clipboard

Copied

I feel like I owe you this link, which is one of Thom Parker's excellent JavaScript Tutorials: https://acrobatusers.com/tutorials/date_time_part2/

You will need to read a bit to grasp the concepts, but this is one of the best tutorials (if not the best) that you will find here in the forums to work around time and date in your PDFs using Acrobat JavaScript..

Community guidelines

Copyright Ā© 2022 Adobe. All rights reserved.