如何将另一个变量添加到脚本中

时间:2016-08-29 10:35:44

标签: javascript datejs

我无法尝试让以下脚本正常运行。我正在尝试将另一个变量添加到脚本中,我因为缺乏编程知识而被攻击了!但是,当我添加名为extraDate的变量时,它似乎不起作用。它没有它,所以我必须在某处出错。

有人可以指出问题是什么吗?

感谢您的帮助。

输出: 使用id =“extraDate”的span,id =“fromDate”和id =“toDate”

脚本: //cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js

完整代码

var fromDate = Date.today().addDays(1);
        if (fromDate.is().saturday() || fromDate.is().sunday()) { 
          fromDate = fromDate.next().monday();
        }
        var toDate = Date.today().addDays(2);
        if (toDate.is().saturday()) { 
          toDate = toDate.next().monday();
        }
        else if (toDate.is().monday()) { 
          toDate = toDate.next().tuesday();
        }
        else if (toDate.is().sunday()) { 
          toDate = toDate.next().tuesday();
        }
        var extraDate = Date.today().addDays(4);
        if (extraDate.is().saturday() || extraDate.is().sunday()) { 
          extraDate = extraDate.next().monday();
        }
        document.getElementById('fromDate').innerHTML = fromDate.toString('dddd dS MMMM');
        document.getElementById('toDate').innerHTML = toDate.toString('dddd dS MMMM');
        document.getElementById('extraDate').innerHTML = extraDate.toString('dddd dS MMMM');

1 个答案:

答案 0 :(得分:0)

您的HTML代码存在问题,因为在我的示例代码段中,您的确切代码可以正常运行:



var fromDate = Date.today().addDays(1);
if (fromDate.is().saturday() || fromDate.is().sunday()) { 
  fromDate = fromDate.next().monday();
}
var toDate = Date.today().addDays(2);
if (toDate.is().saturday()) { 
  toDate = toDate.next().monday();
}
else if (toDate.is().monday()) { 
  toDate = toDate.next().tuesday();
}
else if (toDate.is().sunday()) { 
  toDate = toDate.next().tuesday();
}
var extraDate = Date.today().addDays(4);
if (extraDate.is().saturday() || extraDate.is().sunday()) { 
  extraDate = extraDate.next().monday();
}
document.getElementById('fromDate').innerHTML = fromDate.toString('dddd dS MMMM');
document.getElementById('toDate').innerHTML = toDate.toString('dddd dS MMMM');
document.getElementById('extraDate').innerHTML = extraDate.toString('dddd dS MMMM');

<script src="https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/datejs/date.js"></script>
<p id="fromDate"></p>
<p id="toDate"></p>
<p id="extraDate"></p>
&#13;
&#13;
&#13;