Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Exit

- Home
- Acrobat SDK
- Discussions
- Need help writing time difference, date difference...

- Need help writing time difference, date difference...

0

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/td-p/10378580
Feb 18, 2019
Feb 18, 2019

Copy link to clipboard

Copied

`I am trying to create a pdf with several different functions. 1. I need to calculate the difference in dates to find how many years someone has worked with the company as of April 1, 2019. Second, I need to add the time they will work if they start at 8 am and finish at 5 pm. Third, I need to have a box that needs to read a percentage based on a value inputted into another box (i.e. If a = 15, then b = 45%; if a = 21, b = 50%, etc.). Lastly, I need boxes to read 1% if a "yes" box is checked. I've tried javascript on my own but I am doing something wrong. Can someone please simply tell me how to do this, where to insert my fields, etc.? Nothing I have done has worked. Thanks!`

TOPICS

Acrobat SDK and JavaScript
,
Windows

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Community Expert
,
Feb 19, 2019
Feb 19, 2019

Start by reading these tutorials:

https://acrobatusers.com/tutorials/conditional-execution

https://acrobatusers.com/tutorials/change_another_field

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

If you want someone to do it for you then you'll probably need to pay them for it.

10
Replies
10

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378581#M14706
Feb 19, 2019
Feb 19, 2019

Copy link to clipboard

Copied

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

joyk
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378583#M14708
Feb 19, 2019
Feb 19, 2019

Copy link to clipboard

Copied

Yes, of course! I have been able to use javascript before but this is far more complicated than I am used to doing, and I can't seem to find the error in the formulas I have tried.

First, I need to calculate the hours being worked each day and total it in the contracted hours box. So I created a hidden text box to calculate that time for each day and have tried to include this formula for only MondayStartTime and MondayEndTime:

function dateTimeDiffer(startDTime, endDtime){

var startTime = new Date(TimeStartMonday);

var endTime = new Date(TimeEndMonday);

var diff = endTime.getTime(TimeEndMonday) - startTime.getTime(TimeStartMonday);

var msec = diff;

var hh = Math.floor(msec / 1000 / 60 / 60);

msec -= hh * 1000 * 60 * 60;

var mm = Math.floor(msec / 1000 / 60);

msec -= mm * 1000 * 60;

return hh + “:” + mm + “:”;

}

Second, I need the amount totaled in contracted hours to equal a percentage based on what is entered:

<20 = 45%

21-30 = 50%

31-38 = 55%

>39 = 60%

This is what I have tried so far. (The giant spacing only applied itself when I copied it into this forum).

var n = this.getField("ContractedHours").value;

if (n < 20) {

event.value = "45";

`}`

else if (n >= 21 && n <31 ) {

event.value = "50";

`}`

else if (n >= 31 && n < 39) {

event.value = "55";

`}`

else if (n >= 39) {

event.value = "60";

`}`

Next, I need to add 1% for each "Yes" that is checked. This is the formula I tried to use:

function myFunction() {

var checkBox = document.getElementById("Check Box7");

var text = document.getElementById("Text21");

if (checkBox.checked == true){

text.style.display = "block";

} else {

text.style.display = "none";

}

Last, I need to calculate longevity with the organization based on the hire date compared to the effective date (4/1/2019). If the years equal 5 to 9, they receive an additional percent. 10 or more, they receive 2%. I realize that this is two different formulas and in two different boxes. I do not have a formula for the second part of the process.

- if ( not DateTimeField2.isNull ) then
- var startDate = Date2Num( DateTimeField2, "YYYY-MM-DD" )
- var currentDate = Date()
- var years = ( currentDate - startDate ) / 365
- $ = Floor( years )
- else
- $ = ""
- endif

Thank you for any help you are able to provide. I have tried to figure this out myself but this complex javascript is not clicking for me.

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378582#M14707
Feb 19, 2019
Feb 19, 2019

Copy link to clipboard

Copied

Start by reading these tutorials:

https://acrobatusers.com/tutorials/conditional-execution

https://acrobatusers.com/tutorials/change_another_field

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3

If you want someone to do it for you then you'll probably need to pay them for it.

Community guidelines

joyk
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378584#M14709
Feb 21, 2019
Feb 21, 2019

Copy link to clipboard

Copied

Thank you for linking these tutorials, but I am such a beginner that I do not understand how to use these codes for what I need. Unfortunately, I do not have the authority to pay someone to write the code, either. Do you know of a good course that teaches basics for javascript in pdfs? I have seen so many for building websites, but that is not my goal.

Thanks!

Community guidelines

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378585#M14710
Feb 21, 2019
Feb 21, 2019

Copy link to clipboard

Copied

Only one I'm aware of is: pdfscripting.com

Community guidelines

joyk
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378586#M14711
Feb 26, 2019
Feb 26, 2019

Copy link to clipboard

Copied

try67, Thank you for linking everything above. Those helped solve all but one of my issues.

I've calculated the hours worked using the following script:

var strStart = this.getField("StartMonday").value;

var strEnd = this.getField("EndMonday").value;

strStart = strStart + " 1/1/70";

strEnd = strEnd + " 1/1/70";

if(strStart.length && strEnd.length)

{

var timeStart = util.scand("h:MM tt m/d/yy", strStart);

var timeEnd = util.scand("h:MM tt m/d/yy", strEnd);

if (timeEnd < timeStart) {

var strEnd = this.getField("EndMonday").value

var strEnd = strEnd + " 1/2/70";

var timeEnd = util.scand("h:MM tt m/d/yy", strEnd);

var diff = timeEnd - timeStart;

var oneHour = 60 * 60 * 1000;

var timeleft = (diff/oneHour);

}

var diff = timeEnd - timeStart;

var oneHour = 60 * 60 * 1000;

var timeleft = (diff/oneHour);

event.value = timeleft.toFixed(2);

} else {

event.value = " ";

}

Now I need to subtract the break time for each day if entered. How do I do this?

Second, I need to total all of the hours (with break time excluded) into one field to reveal the total time contracted. How do I do this? I've tried adding the total from each day individually but then I end up with negative or the wrong total overall. Any help you can provide is appreciated.

Thanks!

Community guidelines

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378587#M14712
Feb 26, 2019
Feb 26, 2019

Copy link to clipboard

Copied

Is the break time entered in total time, or as a start time and an end time?

Adding the times should be straight forward. It's just a simple sum...

Community guidelines

joyk
AUTHOR

Explorer
,

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378588#M14713
Feb 26, 2019
Feb 26, 2019

Copy link to clipboard

Copied

Community guidelines

/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378589#M14714
Feb 26, 2019
Feb 26, 2019

Copy link to clipboard

Copied

That is typically caused by an incorrect fields calculation order.

Community guidelines

joyk
AUTHOR

Explorer
,

LATEST
/t5/acrobat-sdk-discussions/need-help-writing-time-difference-date-difference-and-if-then-formulas/m-p/10378590#M14715
Feb 26, 2019
Feb 26, 2019

Copy link to clipboard

Copied

Community guidelines

Copyright © 2023 Adobe. All rights reserved.