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.

kellyl73940005
Participating Frequently
January 14, 2019

Hello,

I need help with a script that takes startdate, starttime and calculates the total time from enddate and endtime.

It works for one day if the startdate and enddates are the same, however as you select two or more days maximum total of 5 it goes and give total time in 24 hours a day. I only need it to to be eight hours or less for one day(same date), two days total of 16 hours.

Any help would be greatly appreciated.

/*
// document level JavaScripts;
function GetField(cName) {
var oField = this.getField(cName);
if(oField ==  null) app.alert("Error accessing field " + cName +
"\nPlease check the field name carefully.", 1, 0);
return oField;
}

function Scand(cFormat, cDate) {
var oDate = util.scand(cFormat, cDate);
if(oDate == null) app.alert("Error converting " + cDate +
" with format " + cFormat, 1, 0);
return oDate;
}

function Time2Num(cFormat, cDate) {
var nMins = null;
var oDate = Scand(cFormat, cDate);
if(oDate != null) {
  nMins = Math.floor(oDate.getTime() / (1000 * 60));
}
return nMins;
}

function Num2Time(cValue) {
return util.printf("%,101.0f:%,102.0f", Math.floor(cValue / 60), cValue % 60);
}
// end document level fucntions;

*/

// custom calculation script;
// date and time formats;
var cDateFormat = "m/d/yy";
var cTimeFormat = "HH:MM";

// get start date and time field values;
var cStartTimeField = "starttime"
var oStartTimeField = GetField(cStartTimeField);
var cStartTimeValue = oStartTimeField.valueAsString;
var cStartDateField = "startdate"
var oStartDateField = GetField(cStartDateField);
var cStartDateValue = oStartDateField.valueAsString;

// get end date and time field values;
var cEndTimeField = "endtime"
var oEndTime = GetField(cEndTimeField);
var cEndTimeValue = oEndTime.valueAsString;
var cEndDateField = "enddate"
var oEndDate = GetField(cEndDateField);
var cEndDateValue = oEndDate.valueAsString;

event.value = "";
// compute time difference if all input fields have data;
if(cStartDateValue != "" &&cStartTimeValue != "" && cEndDateValue != "" && cEndTimeValue != "") {
// convert start date and time values to minutes;
nStart = Time2Num(cDateFormat + " " + cTimeFormat, cStartDateValue + " " + cStartTimeValue);
// convert end date and time values to minutes;
var nEnd = Time2Num(cDateFormat + " " + cTimeFormat, cEndDateValue + " " + cEndTimeValue);
var nDiff = nEnd - nStart - 3840;
event.value = Num2Time(nDiff);
}

try67
Community Expert
January 16, 2019

For a vaction or sick day request can be from .5 upto 8 hours a day.

As in the picture attached it shows five day from a start time to a end time. I would like it to show 40 hours, 8 hrs for each day instead of 8 hours and adding 24 hours for each day.


Sorry, I'm not following you... Maybe someone else can help.

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