1

I am working in Adobe Acrobat DC on a fillable PDF form and new to JavaScript. I need the value to be $100 until 9/21/2018, then from 9/22 - 10/19 to be $125 and then beginning on 10/20 to be $150.

I have the script below which works for the first if statement, but it does not calculate the 10/20/2018 portion of the script. Can someone please help me and tell me what I am doing wrong?

var sub = 100 * Number(this.getField("numEthernet").value);    
var s = this.getField("Date").valueAsString;   
if (s!="") {  
    var d = util.scand("mm/dd/yyyy", s);  
    var cutOffDate = util.scand("mm/dd/yyyy", "9/21/2018");  
    if (d.getTime()>cutOffDate.getTime()){   
        sub *= 1.25;  
    }
}  
else if (s!="") {  
    var d = util.scand("mm/dd/yyyy", s);  
    var cutOffDate = util.scand("mm/dd/yyyy", "10/20/2018");  
    if (d.getTime()>=cutOffDate.getTime()){   
        sub *= 1.50;  
    }  
}
event.value = sub;
Jongware
  • 22,200
  • 8
  • 54
  • 100
Darlhouse
  • 13
  • 5

2 Answers2

2

I'm not familiar with Acrobat DC so i'm not too sure about the availability of some of javascript's basic methods/objects but this should work since I tried to strip down any unnecessary code from my answer:

var sub = 100 * Number(this.getField("numEthernet").value);
var s = this.getField("Date").valueAsString;
if (s != "") {
    var dateFormat = "mm/dd/yyyy";
    var suppliedDate = util.scand(dateFormat, s).getTime();
    if (suppliedDate >= util.scand(dateFormat, "9/22/2018").getTime() && suppliedDate <= util.scand(dateFormat, "10/19/2018").getTime()){
        sub *= 1.25;
    }
    else if (suppliedDate >= util.scand(dateFormat, "10/20/2018").getTime()) {
        sub *= 1.50;
    }
}
event.value = sub;

In the future I would suggest swapping out var s = this.getField("Date").valueAsString with something along the lines of var s = this.getField("Date").valueAsString.trim() so that blank spaces won't cause any issues but i'm not sure if trim is available in Acrobat DC

Rewire
  • 331
  • 2
  • 9
  • Thank you for the help! Unfortunately the $150 is not working. When I select the date of 10/20 it gives me the result of $100...any other suggestions? – Darlhouse Jul 02 '18 at 20:21
  • It works if I select the date of 10/21, but I am getting 100 when I select 10/20... – Darlhouse Jul 02 '18 at 21:32
  • Not sure what happened, but it's working perfectly now. THANK YOU SO MUCH Rewire!!!! – Darlhouse Jul 02 '18 at 22:11
0

It looks like your if statement is checking to see if s is anything other than an empty string. Your else if statement is looking for the same thing, but since your initial if statement already succeeded, it will not look for the else if.

Without knowing your exact syntax, try looking for 2 items:

if(s!="" && /* Check if the current date is within your first time period */) {
  sub *= 1.25;
}
else if(s!="" && /* Check if the current date is within your second time period */) {
  sub *= 1.50;
}

Something along those lines.

Steve
  • 1,384
  • 1
  • 8
  • 7
  • Thank you for the quick response, however I am not sure what to do exactly. Can you be more clear? What do I add after the && that you suggested above? – Darlhouse Jul 02 '18 at 18:52
  • Actually, it looks like Rewire's answer above better answers your question. – Steve Jul 02 '18 at 21:04