我正在設法制作一個日期/周計數器,您可以在這裏增加或減少本週的第一天和最後一天的日期。這是我走到這一步,感謝@ishegg包含日期的週數計數器
<script>
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
curr.setHours(0,0,0,0);
function week() {
curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
// First day of the week
var first = curr.getDate() - curr.getDay();
// Last day of the week
var last = first + 6;
var startDate = new Date(curr.setDate(first));
var endDate = new Date(curr.setDate(last));
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function() {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart)/86400000) + 1)/7);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function startOfWeek(date) {
date.setDate(date.getDate() - date.getDay());
return date;
}
function endOfWeek(date) {
date = startOfWeek(date);
date.setDate(date.getDate() + 6);
return date;
}
function weekPlus() {
var startDate = new Date(startOfWeek(curr));
startDate.setDate(startDate.getDate() + 7);
var endDate = endOfWeek(curr);
endDate.setDate(endDate.getDate() + 7);
curr = startDate;
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function() {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart)/86400000) + 1)/7 + 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
function weekMinus() {
var startDate = new Date(startOfWeek(curr));
startDate.setDate(startDate.getDate() - 7);
var endDate = endOfWeek(curr);
endDate.setDate(endDate.getDate() - 7);
curr = startDate;
document.getElementById("start").innerHTML = startDate;
document.getElementById("end").innerHTML = endDate;
// Week number
Date.prototype.getWeekNumber = function() {
var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart)/86400000) + 1)/7 - 1);
};
document.getElementById("week").innerHTML = ("Week " + curr.getWeekNumber());
}
</script>
<div id="start">start</div>
<div id="end">end</div>
<div id="week">week</div>
<button onclick="week()">current</button>
<button onclick="weekPlus()">add</button>
<button onclick="weekMinus()">substract</button>
按當前 - 按鈕,你會得到的最後和第一天打完包括週數在內。
每次按下按鈕,您都可以添加或減少1周。
問題的代碼:
添加或從其減去周後 - >按當前 -button到周重置爲當前日期,添加或從其減去不啓動從當前一週開始,但從之前按下當前按鈕。按下後添加 -button幾次,然後按。減去,星期數瞬間下降了3,而不是1(同樣的事情逆轉,以3往上走,而不是1 )
每當一年的變化,有時一週跳直奔周2
任何想法是如何解決這些問題的代碼?
任何幫助表示讚賞。
我想這是因爲您使用的是*局部變量*'你一週函數中curr',你可能打算更新*全局變量* curr。解決方案 - 在周內()不要在'curr'前面使用'var' – James
@James謝謝,明白了。任何想法都是爲什麼在添加幾次之後,爲什麼星期數會跳3而不是1,然後減去? – IlariM