Skip to main content
bobd93702072
Known Participant
October 27, 2018
Answered

How do I get the diff between two Date&Time fields and show as Decimal Hrs?

  • October 27, 2018
  • 2 replies
  • 3362 views

I have Date and Time Start "DT1S

I have Date and Time End  "DT1E

I want to DT1E - DT1S and show the result as decimal hours rounded to 1 decimal point as in XX.x

I am using Adobe Pro for Mac on my MacBook Pro using High Sierra.

I have it done on my Excel version of this form, but I want to create a Fillable PDF version of the form and I have searched high and low on how do do this in PDF.

In Excel it is simply =Round((DT1E-DT1S)*24,1) with the result field formatted as a number to 1 decimal place.

Hope someone can make it that easy for me in PDF using "Simplified Field Notation" or last but not least "Custom Calculation Script".

This topic has been closed for replies.
Correct answer gkaiseril

If you work long enough with computers you will have learned several languages that do not stand the test of time.

The following script will compute the difference in minutes and then display the result in hours.

function String2Min(cFormat, cString){

var oDate = util.scand(cFormat, cString); // convert string to Date object;

return Math.floor(oDate.getTime() / (1000 * 60)); // return minutes for Date object;

}

var cDT1S = this.getField("DT1S").value; // get start field value;

var cDT1E = this.getField("DT1E").value; // get end field value;

// covert date strings to minutes and comput difference;

var nDiff = String2Min("mm/dd/yyyy h:MM tt", cDT1E) - String2Min("mm/dd/yyyy h:MM tt", cDT1S);

// format result as hours and set field value;

event.value = Number(util.printf("%,104.1f", (nDiff / 60)));

With some additional changes and naming the output field field in a systematic method one could even compute the input fields do there is no need to edit the scripts when coping the code or the output field.

2 replies

Inspiring
October 27, 2018

Since one needs to use various properties and methods of the JavaScript Date object, one cannot use the simplified field notation which can only use field values and constants. You will also need to write special code for converting date strings to the JavaScript date object and the rounding of floating point values to a specified number of decimal points. You will also need to make a decision if you need to set a field's value to the rounded value or you just want to change the displayed value of the field.

I and others have posted several times about calculating time sheets and date time intervals.

Acrobat does not supply as many functions as Excel, but allows users to write their own custom functions as needed.

You will need to convert your date strings to JavaScript date objects. From the JavaScript date object one can obtain the number of milliseconds  from a common Epoch date, use the "util.scand" method. These values can be converted to hours using simple division and the result can be rounded by using the "util.printf" method.. Once one has the number of hours from the Epoch date, the difference will provide the elapsed time. Since many of steps will be performed more than once, you may want to write your own functions for these steps so one only needs to provide the specific data to be accessed or converted to get a specific result. Using the JavaScript date object will automatically adjust for the intervals that include the changes due to Daylight Savings Time.

bobd93702072
Known Participant
October 28, 2018

gkaiseril,

Thanks for your detailed response. I truly appreciate you taking the time to respond, however, the message is mostly Greek to me. I do very well with Excel but have absolutely no experience in Java Script.

I will however, pick your suggestions apart one at a time and go through the suggested tutorials and try to learn how to put together this one script. I don't know where else to go for help...

gkaiserilCorrect answer
Inspiring
October 28, 2018

If you work long enough with computers you will have learned several languages that do not stand the test of time.

The following script will compute the difference in minutes and then display the result in hours.

function String2Min(cFormat, cString){

var oDate = util.scand(cFormat, cString); // convert string to Date object;

return Math.floor(oDate.getTime() / (1000 * 60)); // return minutes for Date object;

}

var cDT1S = this.getField("DT1S").value; // get start field value;

var cDT1E = this.getField("DT1E").value; // get end field value;

// covert date strings to minutes and comput difference;

var nDiff = String2Min("mm/dd/yyyy h:MM tt", cDT1E) - String2Min("mm/dd/yyyy h:MM tt", cDT1S);

// format result as hours and set field value;

event.value = Number(util.printf("%,104.1f", (nDiff / 60)));

With some additional changes and naming the output field field in a systematic method one could even compute the input fields do there is no need to edit the scripts when coping the code or the output field.

bobd93702072
Known Participant
October 28, 2018

Thank you for responding. I was hoping for a solution but it looks like I will have to learn a new language just to get this one field to work.

If anyone can simplify this process for me, I'm all ears, but in the meantime, I guess I start studying....

try67
Community Expert
Community Expert
October 28, 2018

Well, if you don't want to learn how to write the code you can use this (paid-for) tool I've developed that allows you to set it up easily and quickly (including the option to display the result in decimal format): Custom-made Adobe Scripts: Acrobat -- Calculate Time Differences in a Worksheet