JavaScript Calendar odd behavior
<p>I saw that there were other JS Calendars about but I wanted to try my hand at it and see if I can do it myself.  I ran into a bit of a snag.  Seems that when I run my script to generate this month's (August) calendar I am getting 29 days instead of 31.  Can anyone see what I am doing wrong and point me in the right direction please?</p>

<pre><code>var arrMonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec");
var arrWeek = new Array("Su", "M", "Tu", "W", "Th", "F", "Sa");

var now = new Date();
var thisYear = now.getFullYear();
var thisMonth = now.getMonth();
var thisDate = now.getDate();
var thisDay = now.getDay();



var firstMonth = new Date();
var tempYear = firstMonth.setYear(thisYear);
var tempMonth = firstMonth.setMonth(thisMonth);
var tempDate = firstMonth.setDate(1);

var firstDay = firstMonth.getDay();
var totalDays = numberOfDays(thisYear, thisMonth);

var dayCounter = 1;
var strHTML = "";

strHTML += "&lt;table border='1'&gt;";
strHTML += "&lt;tr&gt;";
for(var x=0; x&lt;arrMonth.length; x++){
    if(x == thisMonth){
        strHTML += "&lt;td colspan='7' align='center'&gt;" + arrMonth[x] + "&lt;/td&gt;";
    }
}
strHTML += "&lt;/tr&gt;";

strHTML += "&lt;tr&gt;";
for(var x=0; x&lt;arrWeek.length; x++){
    strHTML += "&lt;td align='center'&gt;" + arrWeek[x] + "&lt;/td&gt;";    
}
strHTML += "&lt;/tr&gt;";

strHTML += "&lt;tr&gt;";
for(var x=0; x&lt;40; x++){
    if(x &lt; firstDay){
        strHTML += "&lt;td&gt;&amp;nbsp;&lt;/td&gt;";
    }
    else if(x == firstDay){
        strHTML += "&lt;td align='center'&gt;" + dayCounter + "&lt;/td&gt;";
        dayCounter++;
    }
    else{
        if(x &lt;= totalDays){
            if((x % 7) == 0){
                strHTML += "&lt;/tr&gt;";
                strHTML += "&lt;tr&gt;";
                strHTML += "&lt;td align='center'&gt;" + dayCounter + "&lt;/td&gt;";
            }
            else{
                strHTML += "&lt;td align='center'&gt;" + dayCounter + "&lt;/td&gt;";
            }
            dayCounter++;
        }
    }
}
alert(dayCounter + " : " + totalDays);
strHTML += "&lt;/tr&gt;";
strHTML += "&lt;/table&gt;";

alert(strHTML);

function numberOfDays(year, month) { 
    var d = new Date(year, month, 0); 
    return d.getDate(); 
} 
</code></pre>
