1

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:

  1. 17april, 18april, 19april, 20april, 21april, 22april, 23april
  2. 24april, 25april, 26april, 27april, 28april, 29april, 30april
  3. 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

aln447
  • 981
  • 2
  • 15
  • 44
  • JavaScript dates work that way on purpose. When you set the day-of-month to something outside the month, the rest of the date automatically adjusts. So, April 37th is treated as May 7th. – Pointy Apr 17 '16 at 13:33
  • Well this I am aware of, that is why I used them.. – aln447 Apr 17 '16 at 13:35
  • 1
    For one thing, I think it should be `var currentDate = new Date(tDate);` so that your working date *starts* as the target date. – Pointy Apr 17 '16 at 13:40
  • Look at the 2 answer on this [SO post](http://stackoverflow.com/questions/563406/add-days-to-datetime). This explains it a bit further what @Pointy means in the previous comment. – michaPau Apr 17 '16 at 13:51
  • $michaPau it worked! Using the function from the question worked perfectly. Thank you. Post it as an answer please – aln447 Apr 17 '16 at 14:07

0 Answers0