How to calculate time difference between 2 time fields
Copy link to clipboard
Copied
I have a form with a StartTime and EndTime field formatted like HH:MM , and a TotalHours field formatted like (Number, 2 decimal places, separator style 1234.56). In that field I would like to display result of time calculation.
I'm expecting to get such result:
StartTime | EndTime | Total Hours |
---|---|---|
9:15 | 10:30 | 1:15 |
8:30 | 11:15 | 2:45 |
10:05 | 11:30 | 1:25 |
Please help with JavaScript code. Thanks.
Copy link to clipboard
Copied
You don't need to reinvent the wheel, PracticalPDF made it for you: http://practicalpdf.com/the-practicalpdf-date-library-for-adobe-acrobat/
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
Hi JR_Boulay. Thanks for replay.
I just newer in javascript. If I understand correct link just explain how to print formatted date. I need to get calculation result in correct time value. How to do that?
Thanks.
Copy link to clipboard
Copied
I have found that calculating time intervals without the date can lead to issues when this is done in areas the observe Day Light Savings Time. This issue arises if the included time interval include the date and time at which the local time is adjusted. So you might need to take this into account with your form design.
Copy link to clipboard
Copied
I found task that is very similar to my:
http://forum.planetpdf.com/wb/default.asp?action=9&read=64228&fid=5
Just check the last post. The person said he solved the problem. I did like he suggested but my result field still look like decimal number but I would like to get result in format HH:MM. What is skipped in that example?
Thanks.
Copy link to clipboard
Copied
You should read at least the second paragraph:
What it does
The Date Library for Adobe Acrobat adds methods to the native JavaScript Date Object that make it easier to:
- Add days, months, years, or any other time increment to a Date object in a single call… you just pass in a JSON object with the parameters you need.
- Calculate a person’s age either by using a Date Object or a string representing a date.
- Calculate the next business day.
- Calculate the first business day of the next month.
- Use named patterns when formatting date object.
- Detect leap years.
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
I did those steps:
To install the Date Library installer.
- Download the Custom Command file.
- Choose Tools > Action Wizard.
- Select Manage Custom Commands from the toolbar.
- In the Manage Custom Commands dialog box, choose Import and select the .XML file you just downloaded.
- Once you are done, click Close.
What do I need to do to solve my problem?
Thanks
Copy link to clipboard
Copied
What do I need to do to solve my problem?
As far as I can see, you should learn to read the descriptions and instructions until the end.
Acrobate du PDF, InDesigner et Photoshoptographe
Copy link to clipboard
Copied
If you don't want to mess around with writing code, even using Joel's handy library, consider this tool I've developed which allows you to set up such calculations easily and quickly: Custom-made Adobe Scripts: Acrobat -- Calculate Time Differences in a Worksheet
Copy link to clipboard
Copied
It does not handle the DST fall time adjustment.
Copy link to clipboard
Copied
DST is Daylight Saving Time. Is it correct? Is it possible to solve my problem? If yes can somebody help to show sample javascript for that task and briefly explain the code?
Thanks.
Copy link to clipboard
Copied
DateTimeCalculation2 has the time calculations using the JavaScript Date object and splitting the HH:MM approaches. If one includes the date times that include the change from Day Light Savings Time or Summer Time the number of hours will be different than just using the hours and minutes. In the spring adjustment one hour is lost and in the fall adjustment 1 hour is added. These changes reflect the change in the time zone offset for the user's local. It should be noted that the user's computer time zone setting controls how JavaScript will or will not make these adjustments.

