I have this function called week()
, which gives me the current the first (startDate
) and last (endDate
) day of the week and the week number. I also have these two other functions called weekPlus()
and weekMinus()
, which contains variables that add or substract from the variables startDate
/ endDate
by 7 and from the week number by 1.
<script>
// First and last day of the current week
var curr = new Date(new Date().getTime() + 60 * 60 * 24 * 7);
function week() {
var 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 weekPlus() {
// 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 + 7));
var endDate = new Date(curr.setDate(last + 7));
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() {
// 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 - 7));
var endDate = new Date(curr.setDate(last - 7));
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>
So the idea is that when you first click the current button you get the current date of the weeks first and last day and after that you can either add or substract to the dates.
So is there any quick and effective way to increase or decrease the days by 7 and the week number by 1 every time you click the buttons and not just once?
Problems with the code:
Adding or substracting when the month changes, month name doesn't change but stays the same, for example: Sun Oct 29 2017 19:20:14 - Wed Oct 04 2017 19:20:14
When adding or substacting, the week number sometimes jumps by 2, 5 or more.