a341e60348634163d5c3b2ada1c9735f78dba33d83dab6b508f0223a88654d4c

Source Code:



// 祝日1:何月の何日か?
var Holidays1 = new Array(1,1, 1,3, 2,11, 3,21, 4,29, 5,3, 5,4, 5,5, 9,23, 11,3, 11,23, 12,23, 12,30, 12,31);
// 祝日2:何月の第何月曜日か?
var Holidays2 = new Array(1,2, 7,3, 9,3, 10,2);

// 現在の年、月、日の取得
var now = new Date();
var thisYear = now.getFullYear();
var thisMonth = now.getMonth() + 1;
var today = now.getDate();

// 表示年月の記憶
var year = thisYear;
var month = thisMonth;

var monthdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("日", "月", "火", "水", "木", "金", "土");

function showCalen(n){
  month += n;
  if (month == 0) { year--; month=12; }
  else if (month == 13) { year++; month=1; }
  var flag = ((year == thisYear) && (month == thisMonth))? 1: 0;

  var date = new Date(year, month-1, 1);
  var startDay = date.getDay();

  var dateMax = monthdays[month - 1];
  if (month == 2 && ((year%4 == 0 && year%100 != 0) || year%400 == 0)) dateMax = 29;

  // 休日配列の初期化
  var holidays = new Array();
  for (var i=0; i<=dateMax; i++) holidays[i] = 0;

  // 祝日1 の処理
  var firstSunday = (startDay == 0)? 1: 8 - startDay;
  for (i=0; i<Holidays1.length; i+=2) {
    if (Holidays1[i] == month) {
      holidays[Holidays1[i+1]] = 1;
      for (var j=firstSunday; j<dateMax; j+=7)
        if (Holidays1[i+1] == j) holidays[j+1] = 1;  // 振替休日
    }
  }
  // 祝日2 の処理
  var mondays = new Array();
  var firstMonday = (startDay < 2)? 2 - startDay: 9 - startDay;
  for (i=0; i<Holidays2.length; i+=2)
    if (Holidays2[i] == month) holidays[(Holidays2[i+1] - 1) * 7 + firstMonday] = 1;

  var htmlStr = "<table class='calen'>\n"
    + "<tr class='bg1'><th colspan=7>" + year + "年" + month + "月</th></tr>\n";
  htmlStr += "<tr class='bg2'><th class='sun'>" + days[0] + "</th>";
  for (i=1; i<6; i++) htmlStr += "<th>" + days[i] + "</th>";
  htmlStr += "<th class='sat'>" + days[6] + "</th></tr>\n";

  var col=0;
  if (startDay > 0) {
    htmlStr += "<tr>";
    for ( ; col<startDay; col++) htmlStr += "<td> </td>";
  }
  for (i=1; i<=dateMax; i++) {
    if (col == 0) htmlStr += "<tr>";
    if (flag == 1 && i == today) {
        if (holidays[i] == 1 || col == 0) htmlStr += "<td class='today sun'>";
        else if (col == 5) htmlStr += "<td class='today fri'>";
        else if (col == 6) htmlStr += "<td class='today sat'>";
        else htmlStr += "<td class='today'>";
    }
    else if (holidays[i] == 1 || col == 0) htmlStr += "<td class='sun'>";
	else if (col == 5) htmlStr += "<td class='fri'>";
    else if (col == 6) htmlStr += "<td class='sat'>";
    else htmlStr += "<td>";
    htmlStr += i + "</td>";
    if (col == 6) { htmlStr += "</tr>\n"; col=0; } else col++;
  }
  if (col != 0) {
    for ( ; col<7; col++) htmlStr += "<td> </td>";
    htmlStr += "</tr>";
  }
  htmlStr += "</table>";

return (htmlStr);
}
document.getElementById("calen").innerHTML = showCalen(0)+showCalen(1);