【发布时间】:2019-05-18 12:26:13
【问题描述】:
我正在尝试创建一个页面来获取一组按日期排序的 PDF。我似乎无法正确增加日期。我不确定这里出了什么问题。我现在重写了两次代码。没有运气。
当前的问题是为日期设置的变量没有保持整个日期的值。 IE 从 2018 年 12 月 31 日开始递增,或者在 URL 格式为 20181231 的情况下,应生成 urlIncremented=20190101。 2019 年 1 月 1 日,但我的代码结果是 urlIncremented=20181232。
如果设置为 2018 年 6 月 8 日,一个循环的最终结果应该是:url20180608
我在这里搜索了建议,找到了一个名为 Date.JS 的 JS 文件;我已经导入了它,它看起来很有希望,但只是控制台输出了它的一部分代码,即:
function () {
if (this._isSecond) {
this._isSecond=false;
return this;
}
if (this._same) {
this._same=this._is=false;
var o1=this.toObject(),
o2=(arguments[0] || new Date()).toObject(),
v="",
k=j.toLowerCase();
for (var m=(px.length-1); m>-1; m--) {
v=px[m].toLowerCase();
if (o1[v]!=o2[v]) {
return false;
}
if (k==v) {
break;
}
}
return true;
}
if (j.substring(j.length-1)!="s") {
j+="s";
}
return this["add"+j](this._orient);
}
提醒一下,我还不知道 jQuery,我只是在玩它,看看它是否有帮助..
这是我的实际代码。
let url = "blank",
firstRun = true;
/*
function setDateByIncrement(currentSetDate){
let newDate,
currentDate = new Date(),
day = currentDate.getDate()+1,
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear();
console.log(newDate);
newDate = (year+month+day);
console.log(newDate);
return newDate;
}
*/
// use on First run to set the url and date.
//3
function setURL(){
let urlIncremented = url + dateIncrementMethod();
return urlIncremented;
}
// will open x number of new windows containing URL
//2
function grabOpenPDF(maxNumberDays){
let urlSet = setURL();
//Set the variable for max days.
for(let x = 0; x < maxNumberDays; x++){
//window.open(urlSet);
console.log("It works: " + x);
urlSet = setURL();
}
}
/* TODO Add automatic download for MASS print.
function downloadPDF(){
}
*/
//Starts the task.
//1
function start(load){
console.log("Current Address: " + url);
if(load === 1){
console.log("Event load active. ");
let maxDay = document.querySelector('#maxNumberDays').value;;
grabOpenPDF(maxDay);
}else{
console.log("Event load skip. ")
let maxDay = document.getElementById('maxNumberDays').value;
}
}
//4
function dateIncrementMethod(current){
let dateIncrement;
if(firstRun=== true){
var today = new Date($('#date-input').val());
console.log("FirstRun check in 4. ")
}
firstRun = false;
var tomorrow = today.add(1).day;
console.log(tomorrow);
return tomorrow;
}
/* Possibly Deprecated
//let dateIncrement;
let date = new Date($('#date-input').val());
console.log(date);
day = date.getDate() + 1;
if(firstRun === true){
month = date.getMonth() + 1;
year = date.getFullYear();
//dateIncrement = (parseToAPI(year, month, day));
firstRun = false;
parseToAPI(year, month, day);
}else{
day = date.getDate()+1;
parseToAPI(year, month, day);
}
}
*/
function parseToAPI(year, month, day){
let apiDate;
console.log("Entered parse");
this.day = day;
this.month = month;
let d = this.day.toString(),
m = this.month.toString();
if(d.length === 1){
console.log("Entered First IF");
this.day = ('0') + day;
//console.log(day);
}
if(m.length === 1){
console.log("Entered Second IF")
this.month = ('0') + month;
}
apiDate = (year + "" + "" + month + "" + day);
console.log(apiDate);
return apiDate;
}
<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-2.0.3.js"></script>
<script src="https://doc-0k-6g-docs.googleusercontent.com/docs/securesc/77gdpvi38k94jj7nmfcm2n3tq7a0ifhu/ehjuusajghqnne5r2ncfvj30cmbll20p/1545105600000/17500114768188980350/17500114768188980350/1CDff-uWGahZX7aLt6WQfV1-R5PFHwiK8?e=download&nonce=52qkphatg2scm&user=17500114768188980350&hash=3uc9iql9m90vcrv3a7mhg8fdjce1b4fe.js"></script>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="date" id="date-input" required />
<input type="maxNumberDays" id="maxNumberDays" max="31" required />
<button id="startPDFApp" onClick="start()">Print PDFs</button>
<button id="startPDFApp" onClick="start(1)">Load PDFs</button>
<div id="info"></div>
</body>
</html>
【问题讨论】:
-
你想要一个 jQuery 解决方案吗?您也可以考虑为它的 datepicker 使用 jQuery UI,因为它可以返回一个 Date 对象并使生活更轻松一些。我想我知道你的目标是
tomorrow,我猜只要`明天是之前或maxDate,它就会继续循环。我追对了吗? -
我确实需要学习 jQuery,.. 所以如果在新手解释中,请。我可以在明天的某个时候查看 datepicker。简而言之,是的,你是对的。但更重要的是,maxNumberDays 设置为在一定天数后终止循环。此任务的最终结果取决于所使用的按钮,应该会给用户留下大量带有相关 URL 的新标签。
-
也许包括一些说明,说明如何填写它以重现您正在做的任何事情以及所需的行为应该是什么?
-
添加了更多细节,代码还没有完成我只是在寻找一种方法来不断地从预设值增加日期。
-
这里有一个缺失的部分:
tomorrow = new Date(today); tomorrow.setDate( tomorrow.getDate() + 1);而你这里不需要 jQuery
标签: javascript jquery html date increment