他にも JS Calendar があるのを見ましたが、自分でやってみたいと思いました。私はちょっとした障害に遭遇しました。スクリプトを実行して今月 (8 月) のカレンダーを生成すると、31 日ではなく 29 日になっているようです。誰か私が間違っていることを見て、正しい方向に向けてください。
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 += "<table border='1'>";
strHTML += "<tr>";
for(var x=0; x<arrMonth.length; x++){
if(x == thisMonth){
strHTML += "<td colspan='7' align='center'>" + arrMonth[x] + "</td>";
}
}
strHTML += "</tr>";
strHTML += "<tr>";
for(var x=0; x<arrWeek.length; x++){
strHTML += "<td align='center'>" + arrWeek[x] + "</td>";
}
strHTML += "</tr>";
strHTML += "<tr>";
for(var x=0; x<40; x++){
if(x < firstDay){
strHTML += "<td> </td>";
}
else if(x == firstDay){
strHTML += "<td align='center'>" + dayCounter + "</td>";
dayCounter++;
}
else{
if(x <= totalDays){
if((x % 7) == 0){
strHTML += "</tr>";
strHTML += "<tr>";
strHTML += "<td align='center'>" + dayCounter + "</td>";
}
else{
strHTML += "<td align='center'>" + dayCounter + "</td>";
}
dayCounter++;
}
}
}
alert(dayCounter + " : " + totalDays);
strHTML += "</tr>";
strHTML += "</table>";
alert(strHTML);
function numberOfDays(year, month) {
var d = new Date(year, month, 0);
return d.getDate();
}