var now=new Date; var year=now.getFullYear(); var lastmonth=now.getMonth(); var month=now.getMonth()+1; var nextmonth=month+1; var days var begin; function setdate(){//设置日期 if(month==1){ lastmonth=12; } if(month==12){ nextmonth=1; } now.setFullYear(year,month,0) days=now.getDate(); console.log(now.getFullYear()) console.log(now.getMonth()) console.log(days) now.setDate(1); } setdate()将这段代码补全实现日历
时间: 2024-02-01 11:04:43 浏览: 24
好的,我可以帮你补全这段代码,以下是完整的日历实现代码:
```javascript
var now = new Date();
var year = now.getFullYear();
var lastmonth = now.getMonth();
var month = now.getMonth() + 1;
var nextmonth = month + 1;
var days;
var begin;
function setdate() { //设置日期
if (month == 1) {
lastmonth = 12;
}
if (month == 12) {
nextmonth = 1;
}
now.setFullYear(year, month, 0);
days = now.getDate();
console.log(now.getFullYear());
console.log(now.getMonth());
console.log(days);
now.setDate(1);
begin = now.getDay();
var html = '';
html += '<table>';
html += '<tr><td colspan="7">' + year + '年' + month + '月' + '</td></tr>';
html += '<tr><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';
for (var i = 0; i < begin; i++) {
html += '<td></td>';
}
for (var i = 0; i < days; i++) {
if ((i + begin) % 7 == 0) {
html += '<tr>';
}
html += '<td>' + (i + 1) + '</td>';
if ((i + begin + 1) % 7 == 0) {
html += '</tr>';
}
}
html += '</table>';
document.body.innerHTML = html;
}
setdate();
```
这个代码会在页面上生成一个日历表格,你可以根据自己的需要修改样式和布局。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)