Javascript calendar skips days during leapyear?
<p>I wrote a calendar in javascript and jquery (adapted from <a href="http://en.wikicode.org/index.php/Simple_calendar_with_JavaScript" rel="nofollow">"Simple Calendar with Javascript"</a>) that allows me to move back and forward months at a time.</p>

<p>It accounts for leap years by adding in an extra day every 4 years in February, but it seems that an error is being caused because of this.</p>

<p>When you move back in time, or forward, it changes the date and runs the <code>calendar()</code> function according to the new date. Normally, it has 5 rows with 7 columns, each column containing a day. If there isn't enough room with the 5 rows, such as there being 31 days with the 1st being on a Saturday, then it will add an extra 6th row to accommodate.</p>

<p>During a leap year, however, the function doesn't seem to add the extra rows in if the same scenario above happens.</p>

<p>Here's a jsFiddle: <a href="http://jsfiddle.net/charlescarver/DrMWM/" rel="nofollow">http://jsfiddle.net/charlescarver/DrMWM/</a></p>

<p>This gist of <code>calendar()</code>:</p>

<pre><code>function calendar(d){

                $("#calendar").remove();
                $("body").append("&lt;table border='1' id='calendar'&gt;&lt;/table&gt;");

                // Date vars
                var today = new Date(d);
                var month = today.getMonth();
                var day = today.getDay();
                var dayN = today.getDate();
                var week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
                var monthdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
                var days = monthdays[month];
                var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

                // Determine if leap year, if not, set to current year
                if (month &gt; 0) {
                    year = today.getYear();
                    if (year % 4 == 0){
                        days = 29;
                    }
                } else {
                    year = today.getYear();
                }

                // Parse vars
                var jumped = 0;
                var inserted = 1;
                var start = day - dayN%7 + 1;
                if (start &lt; 0) start += 7;
                var weeks = parseInt((start + days)/7);
                if ((start + days)%7 != 0){
                    weeks++;
                }

                $("#calendar").append("&lt;tr&gt;&lt;td class='month' colspan='7'&gt;" + monthNames[month] + " - " + today.getFullYear() + "&lt;/td&gt;&lt;/tr&gt;");

                $("#calendar").append("&lt;tr class='day'&gt;&lt;/tr&gt;");
                for (var i=0; i&lt;7; i++) {
                    $(".day").append("&lt;td&gt;" + week[i][0] + "&lt;/td&gt;");
                }

                function datDate(m, n, y){
                    date = new Date(" " + monthNames[m] + " " + n + " " + y);
                    return date;
                }

                for (var i=weeks; i&gt;0; i--) {
                    if (i === i){
                        $("#calendar").append("&lt;tr class=" + i + "&gt;&lt;/tr&gt;");
                        for (var j=0; j&lt;7; j++) {
                            console.log(inserted);
                            if (jumped&lt;start || inserted&gt;days) {
                                $("." + i).append("&lt;td&gt; &lt;/td&gt;");
                                jumped++;
                            }
                            else {
                                m = month;
                                n = inserted;
                                y = today.getFullYear();
                                date = datDate(m, n, y);
                                if (inserted == dayN) {
                                    $("." + i).append("&lt;td&gt;&lt;a class='calendar_date current_date' data-date='" + date + "'&gt;[" + n + "]&lt;/a&gt;&lt;/td&gt;");
                                } else {
                                    $("." + i).append("&lt;td&gt;&lt;a class='calendar_date' data-date='" + date + "'&gt;" + n + "&lt;/a&gt;&lt;/td&gt;");
                                }
                                inserted++;
                            }
                        }
                    }
                }   
            }
</code></pre>
