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

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

Explorer ,
Oct 26, 2018 Oct 26, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript

Views

1.7K

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

LEGEND , Oct 28, 2018 Oct 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

...

Votes

Translate

Translate
Community Expert ,
Oct 27, 2018 Oct 27, 2018

Copy link to clipboard

Copied

Votes

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
Explorer ,
Oct 27, 2018 Oct 27, 2018

Copy link to clipboard

Copied

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

Votes

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 ,
Oct 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

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

Votes

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
Explorer ,
Oct 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

Thanks TRY67! That is what I was looking for but had no idea of what it would cost. Being this is a work spreadsheet but I'm updating it just for convenience, and for myself, I will venture to try to work through some of the tutorials and see what I can learn. If I have issues or decide to buy the solution, I have saved your site.

Thank you again for your response.

Votes

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
Explorer ,
Oct 29, 2018 Oct 29, 2018

Copy link to clipboard

Copied

Try67,

The link to Part 3 actually takes me back to Part 1. Can you resend the link to part 3 please. These are very helpful tutorials.

Thanks, Bob

Votes

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 ,
Oct 29, 2018 Oct 29, 2018

Copy link to clipboard

Copied

Votes

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
Explorer ,
Oct 29, 2018 Oct 29, 2018

Copy link to clipboard

Copied

Wow! Thank you very much for such a quick response!

Bob de Vries

bobd@calwestaviation.com

Votes

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
LEGEND ,
Oct 27, 2018 Oct 27, 2018

Copy link to clipboard

Copied

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.

Votes

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
Explorer ,
Oct 27, 2018 Oct 27, 2018

Copy link to clipboard

Copied

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

Votes

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
LEGEND ,
Oct 28, 2018 Oct 28, 2018

Copy link to clipboard

Copied

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.

Votes

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
Explorer ,
Oct 29, 2018 Oct 29, 2018

Copy link to clipboard

Copied

Wow! That is fabulous! Thank you so much! It makes sense reading the script lines, but it would take me a while to learn the nuances of all the individual characters needed in the code.

I will study and learn JS but this helps me tremendously.

Thanks again!

Votes

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
Explorer ,
Oct 29, 2018 Oct 29, 2018

Copy link to clipboard

Copied

Wow! Just realized this does exactly what I was looking for including rounding to 1 decimal point. I can't thank you enough for help on this.

I do have on similar but different one I'm trying to work, but with no luck yet.

I have a beginning and ending time (24 Hr Clock) and need the difference in HH:MM in one cell and in HH.1 in another cell

Also sometimes the start time in late PM as in 22:30 and the end time is early morning as in 02:45.

At the end of a column of the resultant HH:MMs, I need to add them up and THEN round to decimal hours, therefore each individual line is not rounded.

I can't expect anyone to do all this for me, and I do want to learn more about JS but any help in getting this done would be greatly appreciated! Thanks.

Votes

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

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);
}

Votes

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
LEGEND ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

You only need to change the Time2Num from computing hours and minutes to hours with decimal hours.

Votes

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 ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

How do I do that ?

Votes

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
LEGEND ,
Jan 14, 2019 Jan 14, 2019

Copy link to clipboard

Copied

One deletes the line that reads:

return util.printf("%,101.0f:%,102.0f", Math.floor(cValue / 60), cValue % 60);

with:

return util.printf("%,104.4f", (cValue / 60));

Votes

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 ,
Jan 15, 2019 Jan 15, 2019

Copy link to clipboard

Copied

Thank you so much!

That worked!

How would one show only 8 hrs vacation for the startdate and enddate when selected, 16 hours for two days selected, 24 hours for three days selected as in example below I need total hours requested to show only 40 hours ?

Thank you so much!

Votes

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 ,
Jan 16, 2019 Jan 16, 2019

Copy link to clipboard

Copied

Can anyone help with this ?

Votes

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 ,
Jan 16, 2019 Jan 16, 2019

Copy link to clipboard

Copied

Your question is not very clear... What exactly are you trying to achieve?

Votes

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 ,
Jan 16, 2019 Jan 16, 2019

Copy link to clipboard

Copied

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.

Votes

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 ,
Jan 16, 2019 Jan 16, 2019

Copy link to clipboard

Copied

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

Votes

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 ,
Jan 16, 2019 Jan 16, 2019

Copy link to clipboard

Copied

LATEST

based on how many days are selected for a vacation, I would like total hours requested to show correct hours based on a eight hour day,

so if I take eight hours I would select the same date in both the start date and end date, total hours requested shows 8 hours if I enter 7:30 into start time and 15:30 into endtime. as in the picture I attached, five days are select with start times and end times.

Based on a eight hour vacation day, it would be 40 hours except my current java code calculates it as 8 hours for the first day and 24 hours each for the other 4 days. I can't get to only add eight hours for each for the 4 other days.

Votes

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