JS生成當月日曆,效果圖javascript
完整代碼:html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <script type="text/javascript"> var months = new Array(); months[0] = "January"; months[1] = "Feburary"; months[2] = "March"; months[3] = "April"; months[4] = "May"; months[5] = "June"; months[6] = "July"; months[7] = "August"; months[8] = "September"; months[9] = "October"; months[10] = "November"; months[11] = "December"; var cur_date = new Date(); var cur_month = cur_date.getMonth(); cur_date.setDate(1); document.write("<table border=1 cellpadding=3 cellspacing=0>"); document.write("<tr>"); document.write("<td colspan=7 align='center'>" + months[cur_month] + "</td>"); document.write("<tr>"); document.write("<td align='center'>S</td>"); document.write("<td align='center'>M</td>"); document.write("<td align='center'>T</td>"); document.write("<td align='center'>W</td>"); document.write("<td align='center'>T</td>"); document.write("<td align='center'>F</td>"); document.write("<td align='center'>S</td>"); document.write("</tr>"); if (cur_date.getDay() != 0) {//若是1號不是星期天 document.write("<tr>"); for (i = 0; i < cur_date.getDay(); i++) { document.write("<td> </td>");}//輸出1號前面空白的星期 } while (cur_date.getMonth()== cur_month) { if (cur_date.getDay == 0) {//星期天時換行<tr> document.write("<tr>"); } document.write("<td align='center'>" + cur_date.getDate() + "</td>"); if (cur_date.getDay() == 6) { document.write("</tr>");//星期六時換行結束</tr> } cur_date.setDate(cur_date.getDate() + 1); } for(var i=cur_date.getDay();i<=6;i++){//輸出月最後一天後面空白的星期 document.write("<td> </td>"); } document.write("</table>"); </script> </body> </html>