## Need help writing time difference, date difference, and if/then formulas

Community Beginner ,
Feb 18, 2019

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

Views

298

Likes

Report

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

## Need help writing time difference, date difference, and if/then formulas

Community Beginner ,
Feb 18, 2019

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

Views

299

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 18, 2019 0
10 Replies 10
Most Valuable Participant ,
Feb 19, 2019

Copied

Please share your work so far, so we can help you understand what is wrong with it, so you learn from fixing it. Thanks. Please Post not only your code but exactly where each piece of code is added (e.g. "OnBlur action of field Zip").

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 19, 2019 0
Community Beginner ,
Feb 19, 2019

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.

1. if ( not DateTimeField2.isNull ) then
2.     var startDate = Date2Num( DateTimeField2, "YYYY-MM-DD"
3.     var currentDate = Date()
4.
5.     var years = ( currentDate - startDate ) / 365
6.     \$ = Floor( years )
7. else
8.     \$ = ""
9. 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.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 19, 2019 0
Most Valuable Participant ,
Feb 19, 2019

Copied

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 19, 2019 1
Community Beginner ,
Feb 21, 2019

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!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 21, 2019 0
Most Valuable Participant ,
Feb 21, 2019

Copied

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 21, 2019 0
Community Beginner ,
Feb 26, 2019

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!

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2019 0
Most Valuable Participant ,
Feb 26, 2019

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

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2019 0
Community Beginner ,
Feb 26, 2019

Copied

Thank you! I realized what I was doing wrong when entering data into the form to test it out and collect the sum of the hours worked and break time. Sometimes I still have issues with fields automatically updating based on the data entered, but it will update when something else is selected. Is there any reason for this or a way to avoid it?

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2019 0
Most Valuable Participant ,
Feb 26, 2019

Copied

That is typically caused by an incorrect fields calculation order.

Likes

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2019 1
LATEST
Community Beginner ,
Feb 26, 2019