Skip to main content
Known Participant
February 19, 2019
Answered

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

  • February 19, 2019
  • 2 replies
  • 1878 views

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!

This topic has been closed for replies.

2 replies

Known Participant
February 21, 2019

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!

try67
Community Expert
Community Expert
February 21, 2019

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

Legend
February 19, 2019

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

Known Participant
February 19, 2019

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.