js万年历

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>万年历</title>
    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }


        #box {
            margin: 50px auto;
            width: 500px;
            height: 300px;
            border: 1px solid deepskyblue;
            border-radius: 4px;
        }

        #top {
            padding: 20px;
            border-bottom: 2px solid dodgerblue;
        }

        #top select {
            margin-right: 10px;
        }

        #top #query {
            width: 60px;
            margin-left: 20px;
        }

        #con {
            width: 100%;
        }

        #con #week {
            width: 100%;
        }

        #week li {
            display: inline-block;
            width: 13%;
            font-size: 14px;
            font-family: "微软雅黑";
            text-align: center;
        }
        #con #day{
            width: 100%;
        }
        #day li{
            display: inline-block;
            width: 13%;
            text-align: center;
        }

    </style>
</head>
<body>
<div id="box">
    <div id="top">
        <select name="year" id="year">
            <option value="">请选择年份</option>
        </select>
        <label for="year"></label>

        <select name="month" id="month" title="">
            <option value="">请选择月份</option>
        </select>
        <label for="month"></label>
        <button id="query">查询</button>
    </div>
    <div id="con">
        <ul id="week">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <ul id="day">
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>
<script>
    window.onload = function () {
        var listYear = document.getElementById("year");
        var listMonth = document.getElementById("month");
        var btn_query = document.getElementById("query");
        var dayList = document.getElementById("day").getElementsByTagName("li");

        /*添加年*/
        for (var i = 1995; i < 2018; i++) {
            var option = document.createElement("option");
            option.value = i;
            option.innerHTML = i;
            listYear.appendChild(option);
        }

        /*添加月*/
        for (var j = 1; j < 13; j++) {
            var newOption = document.createElement("option");
            newOption.value = j;
            newOption.innerHTML = j;
            listMonth.appendChild(newOption);
        }

        /*判断是否为闰年*/
        /*公元年数能被4整除但不能被100整除或者能被400整除的为闰年其他为平年*/
        function isLeap(year) {
            /*res标记0为平年,1为闰年*/
            var res = 0;
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    res = 1;
                } else {
                    res = 0;
                }
            } else {
                if (year % 4 == 0) {
                    res = 1
                } else {
                    res = 0;
                }
            }
            return res;

        }

        /*存储一个月的天数数组*/
        var m_day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

        btn_query.addEventListener("click", a);
        function a() {
            /*清空上一个月残留的数字*/
            for(var i =0;i<dayList.length;i++){
                dayList[i].innerHTML='';
            }

            if (listYear.value == '' || listMonth.value == '') {
                alert("请选择年份或月份")
            } else {
                var year = parseInt(listYear.value);
                /*默认日期的月份是从0月开始的,而获取的月份的值是从1开始的,因此要减一*/
                var month = parseInt(listMonth.value)-1;
                if (isLeap(year) == 1) {
                    m_day[1] = 29;
                } else if (isLeap(year) == 0) {
                    m_day[1] = 28;
                }
                /*获取某年某月的第一天,直接使用初始化日期的方法初始化这一天*/
                var someDay =new Date(year,month,1);

                /*获取每个月的第一天是星期几*/
                var firtDay = someDay.getDay();

                /*这里的month已经减1*/
                for(var i =0;i<m_day[month];i++){
                    /*判断每个月的第一天是不是星期天,如果是从第二行显示*/
                    if (firtDay==1){
                        dayList[i+firtDay+6].innerHTML=i+1;
                    }else if (firtDay==0){
                        dayList[i+firtDay+6].innerHTML=i+1;
                    }else{
                        dayList[i+firtDay-1].innerHTML=i+1;
                    }
                }

            }
        }
    }
</script>
</body>
</html>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 JavaScript 实现万年历的代码: ```javascript function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } function getDaysInMonth(year, month) { if (month === 2) { return isLeapYear(year) ? 29 : 28; } else if ([4, 6, 9, 11].includes(month)) { return 30; } else { return 31; } } function getDayOfWeek(year, month, day) { const days = ["日", "一", "二", "三", "四", "五", "六"]; const date = new Date(year, month - 1, day); return days[date.getDay()]; } function getCalendar(year, month) { const daysInMonth = getDaysInMonth(year, month); const firstDayOfWeek = getDayOfWeek(year, month, 1); const calendar = []; let week = [null, null, null, null, null, null, null]; for (let day = 1, i = 0; day <= daysInMonth; day++, i++) { week[i] = day; if (getDayOfWeek(year, month, day) === "六" || day === daysInMonth) { calendar.push(week); week = [null, null, null, null, null, null, null]; i = -1; } } return { calendar, firstDayOfWeek }; } function formatMonth(month) { return month < 10 ? "0" + month : month; } function createCalendarTable(year, month) { const { calendar, firstDayOfWeek } = getCalendar(year, month); let table = "<table>"; table += "<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>"; for (let week of calendar) { table += "<tr>"; for (let day of week) { if (day === null) { table += "<td></td>"; } else { table += `<td>${day}</td>`; } } table += "</tr>"; } table += "</table>"; return table; } function createCalendar(year, month) { const { calendar, firstDayOfWeek } = getCalendar(year, month); const monthStr = `${year}年${formatMonth(month)}月`; let calendarHtml = ` <div class="calendar"> <div class="month">${monthStr}</div> ${createCalendarTable(year, month)} </div> `; return calendarHtml; } ``` 使用方法: ```javascript // 创建 2021 年 10 月的万年历 const calendarHtml = createCalendar(2021, 10); // 将 HTML 插入到页面中 document.body.innerHTML = calendarHtml; ``` 这样就可以在页面上显示出 2021 年 10 月的万年历了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值