カレンダー(前月・次月へのリンク付)
<html> <head> <title>カレンダー</title> </head> <body> <table align="center" border="5" bordercolor="#339999" cellpadding="8" cellspacing="0"> <script language="JavaScript"> <!-- if(location.search.indexOf("/") > 0) { // 年月取得 query = new Array(); query = location.search.split("/"); year = parseInt(query[1]); // 年 month = parseInt(query[2]); // 月 } else { today = new Date(); year = today.getFullYear(); // 今日の年 month = today.getMonth() + 1; // 今日の月 } start = new Date(year + "/" + month + "/1"); startday = start.getDay(); // 1日の曜日 days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if(month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) { days[1]++; // うるう年 } enddate = days[month - 1]; // 最後の日 prevyear = year; // 前月・次月 prevmonth = month - 1; nextyear = year; nextmonth = month + 1; if(prevmonth < 1) { prevyear--; prevmonth += 12; } else if(nextmonth > 12) { nextyear++; nextmonth -= 12; } document.write('<tr>'); document.write('<td colspan="7" align="center">'); document.write('<a href="./cal?/', prevyear, '/', prevmonth, '/" style="font-size:10px">≪前月</a> '); document.write(year, '<span style="font-size:10px">年</span>'); document.write(month, '<span style="font-size:10px">月</span> '); document.write('<a href="./cal?/', nextyear, '/', nextmonth, '/" style="font-size:10px">次月≫</a>'); document.write('</td>'); document.write('</tr>'); document.write('<tr>'); document.write('<td align="center" style="font-size:10px;color:red">日</td>'); document.write('<td align="center" style="font-size:10px">月</td>'); document.write('<td align="center" style="font-size:10px">火</td>'); document.write('<td align="center" style="font-size:10px">水</td>'); document.write('<td align="center" style="font-size:10px">木</td>'); document.write('<td align="center" style="font-size:10px">金</td>'); document.write('<td align="center" style="font-size:10px;color:blue">土</td>'); document.write('</tr>'); count = 0; for(i = 0 ; i < startday ; i++) { // 1日の曜日までの空欄 if(count % 7 == 0) { document.write('<tr>'); } document.write('<td> </td>'); ++count; } for(i = 1 ; i <= enddate ; i++) { // 日付を書き出す if(count % 7 == 0) { document.write('<tr>'); } document.write('<td align="right"'); if(count % 7 == 0) { document.write(' style="color:red"'); } if(count % 7 == 6) { document.write(' style="color:blue"'); } document.write('>', i, '</td>'); ++count; if(count % 7 == 0) { document.write('</tr>'); } } while(count % 7 != 0) { // 最後の日から土曜までの空欄 document.write('<td> </td>'); ++count; if(count % 7 == 0) { document.write('</tr>'); } } //--> </script> </table> </body> </html>
〔 実行する 〕