首页 > 代码库 > JS生成当月日历

JS生成当月日历

JS生成当月日历,效果图

完整代码:

<!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>&nbsp;</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>&nbsp;</td>");
}

document.write("</table>");
</script>
</body>
</html>