javascriptif-statementpdfadobegettime

Using getTime to calculate multiple Dates with If Else statements


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;

Solution

  • 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