Good evening stack. I have this, half made, half copied function which in theory should take a desired date, and append it in a div with 7 next days ahead:
//GLOBAL VARIABLES
var startDate = new Date();
var nextDate = new Date();
var prevDate = new Date();
function GetDates(tDate) {
var aryDates = [];
$("#calendar").empty();
for(var i = 0; i <= 7; i++) {
var currentDate = new Date();
currentDate.setDate(tDate.getDate() + i);
$("#calendar").append("<div class='calendar-day'><p class='weekday'>"+DayAsString(currentDate.getDay())+"</p><p class='day'>"+currentDate.getDate()+"</p><p class='month'>"+MonthAsString(currentDate.getMonth())+"</p><p class='daytime'>Rano</p><p class='daytime'>Popołudnie</p><p class='daytime'>Wieczór</p></div>");
}
nextDate.setDate(tDate.getDate() + 7); //THIS CAUSES THE PROBLEM
prevDate.setDate(tDate.getDate() - 7); //THIS CAUSES THE PROBLEM
console.log("Next Date: " + nextDate);
console.log("Previous Date: " + prevDate);
}
And is fired from jquery document ready:
$(document).ready(function(){
GetDates(startDate); wartosciami od dnia dzisiejszego
var navright = $(".calendar-nav-right");
var navleft = $(".calendar-nav-left");
navright.on("click", function(){GetDates(nextDate);});
navleft.on("click", function(){GetDates(prevDate);});
//console.log(aryDates);
});
The problem lies specifically in the two setDate lines with a comment. It works completely fine untill the result might go beyond a specific calendar month (ie. for april, it will go nuts if the result is > 30). In the current setting, if I'd click the navleft button causing the calendar to go forward a few times, and print the value of "nextDate" it show me the correct date, yet the dates itself would go in this order:
- 17april, 18april, 19april, 20april, 21april, 22april, 23april
- 24april, 25april, 26april, 27april, 28april, 29april, 30april
- 1april, 2april, 3april, 4april, 5april, 6april, 7april
The only moments when it would shift to another month would be when it fires of on the way, like this:
- 28april, 29april, 30april, 1may, 2may, 3may, 4may
And when I'd start mixing the next and previous calendar, it gets even more messed up with the starting dates.
I've been seeting at this for 3 hours now and can't find a way to make it work properly. The error can be found HERE under the tab "2) Wybierz Date"
Any help on the topic would be gratefuly appreciated