【问题标题】:Using getTime to calculate multiple Dates with If Else statements使用 getTime 通过 If Else 语句计算多个日期
【发布时间】:2018-12-11 00:23:42
【问题描述】:

我正在 Adob​​e Acrobat DC 中处理可填写的 PDF 表单,并且是 JavaScript 新手。我需要价值在 2018 年 9 月 21 日之前为 100 美元,然后从 9/22 到 10/19 为 125 美元,然后从 10/20 开始为 150 美元。

我有下面的脚本,它适用于第一个 if 语句,但它不计算脚本的 10/20/2018 部分。有人可以帮助我并告诉我我做错了什么吗?

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;

【问题讨论】:

    标签: javascript if-statement pdf adobe gettime


    【解决方案1】:

    看起来您的 if 语句正在检查 s 是否不是空字符串。您的 else if 语句正在寻找相同的内容,但由于您的初始 if 语句已经成功,它不会寻找 else if。

    在不知道您的确切语法的情况下,尝试查找 2 项:

    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;
    }
    

    类似的东西。

    【讨论】:

    • 感谢您的快速回复,但我不知道具体该怎么做。你能更清楚一点吗?我在上面建议的 && 之后添加什么?
    • 实际上,看起来 Rewire 上面的回答更好地回答了你的问题。
    【解决方案2】:

    我不熟悉 Acrobat DC,所以我不太确定 javascript 的一些基本方法/对象的可用性,但这应该可以工作,因为我试图从我的答案中删除任何不必要的代码:

    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;
    

    以后我会建议将var s = this.getField("Date").valueAsString 换成类似于var s = this.getField("Date").valueAsString.trim() 的东西,这样空格就不会造成任何问题,但我不确定 Acrobat DC 中是否可以使用 trim

    【讨论】:

    • 感谢您的帮助!不幸的是,150 美元不起作用。当我选择 10/20 的日期时,它给了我 100 美元的结果......还有其他建议吗?
    • 如果我选择 10/21 的日期,它可以工作,但是当我选择 10/20 时我得到 100...
    • 不确定发生了什么,但它现在运行良好。非常感谢重新布线!!!!
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多