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

Simple Time Calculation for PDF

Community Beginner ,
May 02, 2017 May 02, 2017

Copy link to clipboard

Copied

This is not a question, but a post for being able to do simple time calculation for time sheets that I have come up with that works on both PCs and iOS that does not get into the complicated time functions of Java. This way uses the drop down menus and the export value ability when you add items to that menu. First you will want to create 7 drop down menus in your PDF. Name three of them for StartTime (StartTimeHour or STH; StartTimeMin or STM; StartTimeAP; or STAP), three for EndTime (EndTimeHour or ETH; EndTimeMin or ETM; EndTimeAP; or ETAP), and one Break. If you want you can add 1 to these or just remember to add the number to each new one you start for each line. Now in the drop down boxes for the hours put 1 in the "Item" box and 1 in the "Export Value" box right below then add to the list, do this each for each hour 2-11 with the export value corresponding to the item. NOTE: for hour 12 make sure export value is 0!! This is key for the Java script later. Before closing make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" is unchecked. Next do the drop down boxes for the minutes, we round to the nearest 15 minutes, so "Item" box :15 has "Export Value" box of 0.25; :30 is 0.5; :45 is 0.75; :00 is 0. Make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" are unchecked. Next do the drop down boxes "StartTimeAP" and "EndTimeAP" as this will be your AM, export value 0, and PM, export value 12. Again make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" are unchecked. Finally in the "Break" drop down menu we have the option from None to 2 hours in 15 minute blocks e.g. None is 0, 0:30 is 0.5, 1:15 is 1.25, etc. Again make sure the "Sort items" and "Commit selected value immediately" boxes are checked and "Allow user to enter custom text" and "Check Spelling" are unchecked. Now with those done create a Text Field box and name it TotalWorked, no need to shorten as it will not be referenced in the Java. Now in the Properties of the "TotalWorked" box go to the Calculate tab and choose "Custom calculation script" and click the "Edit" button. Here you will copy in the following Java script:

//Get Time Values

var StartTimeHour = this.getField("STH1").value;

var StartTimeMin = this.getField("STM1").value;

var StartTimeAP = this.getField("STAP1").value;

var EndTimeHour = this.getField("ETH1").value;

var EndTimeMin = this.getField("ETM1").value;

var EndTimeAP = this.getField("ETAP1").value;

var BreakTime = this.getField("Break1").value;

//Combine Time Values

var StartTime = (StartTimeHour + StartTimeMin + StartTimeAP);

var EndTime = (EndTimeHour + EndTimeMin + EndTimeAP);

//Calculate Time

var TimeWorked = ((EndTime - StartTime) - BreakTime);

//Test for Midnight Crossover

if(TimeWorked < 0) {// Shift 24 hours

TimeWorked += 24;

} else if(TimeWorked === 0) {//Shift 24 hours for same times

TimeWorked += 24;

}

//Print Results

event.value = TimeWorked;

And that's it. When you do the preview you will see that all the time calculations come out correct for the total number of hours worked. Again this has been confirmed to work with no issues on PC and iOS, i would assume Android and OSX as well since there is no complicated Java scripting but I have not confirmed since we do no use either of those OS. Also, if you don't round by the nearest 15 minutes you could calculate out what you do round to based on the hour, e.g. 10 min would be 0.1667 hours. However, as you can see this causes repeating numbers and you would have to have to add a round up function into the Java script to compensate, or if not too picky could choose the two decimal option for the TimeWorked box and let the PDF do the rounding. Finally, I have an even simpler way of doing time base on just a StartTime and EndTime drop down. However, this generates very larger drop down boxes and can be a bit difficult to manage on smaller screens. If anyone is interested I can also post that on here as well. I hope this  helps out a lot of people since doing time can be so difficult to navigate on PDF. Thanks

TOPICS
PDF forms

Views

11.8K

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 ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Hi.

 

This is not a question

Cool.

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 ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

It appears you are trying to compute an elapsed time interval. I see not allowance for the hour change for Daylight Savings Time or Summer Time. Have you looked at the series:

https://acrobatusers.com/tutorials/date_time_part1

Working with date and time in Acrobat JavaScript (Part 1 of 3)

Working with date and time in Acrobat JavaScript (part 2 of 3)

Working with date and time in Acrobat JavaScript (part 3 of 3)

by Thom Parker.

The described process utilizes the user's computer to keep track of the timezone offset and DST adjustment to display the local time. This approach is used by the communications and transpiration industries, military organizations, and many scientific disciplines, especially astronomy, that operate on a world wide or need  to coordinate with world wide military units. In the days of paper records the UTC (GMT) time was written along with the local timezone offset adjusted for DST. This is all done behind the scenes with you computer.

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 ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

gkaiseril as stated in my post this script is not using date and time Java functions, but rather simply adding values together to get time from the Drop Down box (not the Date/Time in pdf). What you have suggested I looked at and this coding as noted by many in other forums has issues in iOS and often ends up incorrectly giving the time worked. I came up with this method for those that have a little understanding, like me, and are looking to do simple time sheets in pdf.

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 ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

You should be aware that at least once a year for a specific time interval this code will not work correctly.

I think pressure should be brought to bear on the app developers to make the PDF reader more robust and support more JavaScript features. After all the devices clocks work properly as to the GPS applications so why not time calculations in user written for PDF forms. I am sure that a time sheet app written for the mobile app would work properly.

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 12, 2019 Oct 12, 2019

Copy link to clipboard

Copied

Hi,

 

I would love ot be able to do this because the pdf we have now only works on PC and not iOS. Did you say you'd post the one you have? If so, I would love a copy to adapt mine.

 

Thanks,

 

AR

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
New Here ,
Mar 25, 2020 Mar 25, 2020

Copy link to clipboard

Copied

This is a great script that I am trying to use for my own time sheet set up. Is there away to make the hrs 0 so at the start of use of the form there is no hours showing? or no time added? Currently it seems that all numerics are used to not allow this to be. Any help or advise would be great.

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 ,
May 19, 2020 May 19, 2020

Copy link to clipboard

Copied

What are the results you are seeing? If the inputs are blank, then the output should be zero.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Aug 23, 2022 Aug 23, 2022

Copy link to clipboard

Copied

I hope this is still active. I appreciate this script but my TotalWorked shows 24 when there are no entries in the fields.

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 ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Please post your question and the script you are using to a new thread. Also provide the input values you are using for testing. 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
New Here ,
Sep 30, 2022 Sep 30, 2022

Copy link to clipboard

Copied

Thank you for this and for the most part it does what I need it to but, when I try to add more hours on a particular work day over a 9 day period so that the 10 work day is zero hours, I can't seem to figure out how to do this.  Adding option in the drop down menu for item 0 and export value 0 seems to add hours! I need to add hours within a 5 day work week over a 2 week period.  The 10 days need to add up to 75 hours.  If I want a day off on Friday (the 10th work day), I will need to add a few hours beyound the regular 7.5 hrs per work day.  How would I manage this feat?!  

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
New Here ,
May 17, 2020 May 17, 2020

Copy link to clipboard

Copied

Hi!

Thank you for the script provided. I tried using it for myself but it say that my format is not correct. What should the format be in this scenario?

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
New Here ,
May 17, 2020 May 17, 2020

Copy link to clipboard

Copied

This is the script I've entered:

var startTime = this.getField("SHIFT_START_TIMERow1").value;
var endTime = this.getField("SHIFT_END_TIMERow1").value;
var breakTime = this.getField("Break1").value;
var timeWorked = (endTime - startTime);
if (timeWorked < 0) {
timeWorked = 24 + timeWorked;
}
this.getField("TOTAL_TIME_MINUS_BREAKRow1").value = timeWorked - breakTime;

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 ,
May 19, 2020 May 19, 2020

Copy link to clipboard

Copied

So what are the values entered into your start, end, and break fields? If they are not straight numbers they will have to be broken into usable parts before used in a calculation. That's a big point made in the earlier posts. You should also read some the the links that are posted to learn about handling time values. 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

This was very helpful, but its not doing the calculation. what could be the reason. Ive entered the code per your instruction.

//Get Time Values
var StartTimeHour = this.getField("STH1").value;
var StartTimeMin = this.getField("STM1").value;
var StartTimeAP = this.getField("STAP1").value;
var EndTimeHour = this.getField("ETH1").value;
var EndTimeMin = this.getField("ETM1").value;
var EndTimeAP = this.getField("ETAP1").value;
var BreakTime = this.getField("Break1").value;
//Combine Time Values
var StartTime = (StartTimeHour + StartTimeMin + StartTimeAP);
var EndTime = (EndTimeHour + EndTimeMin + EndTimeAP);
//Calculate Time
var TimeWorked = ((EndTime - StartTime) - BreakTime);
//Test for Midnight Crossover
if(TimeWorked < 0) {// Shift 24 hours
TimeWorked += 24;}
else if(TimeWorked === 0) {//Shift 24 hours for same times
TimeWorked += 24;}
//Print Results
event.value = TimeWorked;

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 23, 2023 Jan 23, 2023

Copy link to clipboard

Copied

This is a terrible method for doing time calculations. I would suggest reading about performing actual time calculations. There are many posts on this forum that show good techniques for getting time differences. 

 

But, if you want to debug this method, you'll need to provide more information. What exactly isn't working?  Are there any errors reported in the console window? Have you tried runninging this script line by line in the console to see where it falls apart?

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 24, 2023 Jan 24, 2023

Copy link to clipboard

Copied

Hello Thom,
Thanks for your response
Unfortunately, I’m not versed on script so I found this post and thought I could make it work.
There are no errors at all, after changing the time, the final cell where the script is don’t return a result.
The only issue I did find on the article was the name of the result cell where it says to name it one way, but the script has a different name
I just need a simple time tracking form and total at the end, I could do it in excel but it’s not practical for mobile use

Larry

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 24, 2023 Jan 24, 2023

Copy link to clipboard

Copied

Check the JS Console. You have errors in your code.

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 24, 2023 Jan 24, 2023

Copy link to clipboard

Copied

LATEST

Basically, none of the field names in your code match the actual names in the file...

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