美文网首页
JS计算指定日期是距今的第几周,星期几

JS计算指定日期是距今的第几周,星期几

作者: 回忆不死我们不散 | 来源:发表于2020-07-07 16:49 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
        <input id="RFLOT_DATE" type="date"/>
        <input id="RFLOT_LOT" />
        <script>
            /**
            * 判断年份是否为润年
            *
            * @param {Number} year
            */
            function isLeapYear(year) {
                return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
            }
            /**
            * 获取某一年份的某一月份的天数
            *
            * @param {Number} year
            * @param {Number} month
            */
            function getMonthDays(year, month) {
                return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
            }
            /**
            * 获取某年的某天是第几周
            * @param {Number} y
            * @param {Number} m
            * @param {Number} d
            * @returns {Number}
            */
            function getWeekNumber(y, m, d) {
                var now = new Date(y, m - 1, d),
                    year = now.getFullYear(),
                    month = now.getMonth(),
                    days = now.getDate();
                //那一天是那一年中的第多少天
                for (var i = 0; i < month; i++) {
                    days += getMonthDays(year, i);
                }
    
                //那一年第一天是星期几
                var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
    
                var week = null;
                if (yearFirstDay == 1) {
                    week = Math.ceil(days / yearFirstDay);
                } else {
                    days -= (7 - yearFirstDay + 1);
                    week = Math.ceil(days / 7) + 1;
                }
    
                return week;
            }
            var lot;
            function loaddate() {
                var now = new Date();
                var str = now.getFullYear() + "-" + ((now.getMonth() + 1) < 10 ? "0" : "") + (now.getMonth() + 1) + "-" + (now.getDate() < 10 ? "0" : "") + now.getDate();
                var year = now.getFullYear();
                var month = ((now.getMonth() + 1) < 10 ? "0" : "") + (now.getMonth() + 1);
                var date = (now.getDate() < 10 ? "0" : "") + now.getDate();
                document.getElementById("RFLOT_DATE").value = str;
                var ttwo = year.toString().substring(2, year.length);
                lot = ttwo + month + date;
                console.log(year,month,date,ttwo)
                console.log(getWeekNumber(year,month,date),ttwo)
                document.getElementById("RFLOT_LOT").value=getWeekNumber(year,month,date)+ttwo;
    
            }
            loaddate()
        </script>
    
    
    
    <script>
        (function(){
                /*
                * 说明:
                * 一周的起始计算方式不同国家有所不同,很多其他国家将周日作为一周的开始
                * 本代码使用中国习惯,将周一作为每周的开始
                * 特此说明
                */
    
                function TodayInfo(start) {
                    var WEEKLEN = 7, // 一周7天为常量
                        WEEKDAYS = ["日", "一", "二", "三", "四", "五", "六"],
                        weekInfo = {"week": null, "day": null}, // 初始化返回信息,默认第null周,星期null
                        oneDay = 24 * 60 * 60 * 1000, // 一天的毫秒时长
                        weekLeave, // 开学当天所在周剩余天数
                        weekStart, // 开学当天start是星期几
                        today, // 今天
                        dateDiff, // 今天与开学当天日期差
                        sDate; //开学之日,日期对象
                    var rDateStr = /\d{4}[\/-]\d{1,2}[\/-]\d{1,2}/g; // 简单的日期格式校验:2013/12/19
                    if (!rDateStr.test(start)) {
                        alert("请使用合法的开学日期!!!");
                        return weekInfo;
                    }
                    sDate = new Date(start.replace("-", "/"));
                    weekStart = sDate.getDay();
                    weekStart = weekStart === 0 ? 7 : weekStart; // JS中周日的索引为0,这里转换为7,方便计算
    
                    weekLeave = WEEKLEN - weekStart;
                    today = new Date();
                    weekInfo.day = WEEKDAYS[today.getDay()];
                    today = new Date(today.getFullYear() + "/" + (today.getMonth() + 1) + "/" + today.getDate());
                    dateDiff = today - sDate;
                    if (dateDiff < 0) {
                        alert("别开玩笑了,你还没开学呢!!!");
                        return weekInfo;
                    }
                    dateDiff = parseInt(dateDiff / oneDay);
                    weekInfo.week = Math.ceil((dateDiff - weekLeave) / WEEKLEN) + 1;
                    return weekInfo;
                }
                // 测试结果
                var td = TodayInfo("2020/1/1");
                console.log("今天是自2013/12/16日,开学以来的第 " + td.week + " 周,今天星期" + td.day);
                td = TodayInfo("2013/11/11");
                console.log("今天是自2013/11/11日,开学以来的第 " + td.week + " 周,今天星期" + td.day);
                td = TodayInfo("2013/09/01");
                console.log("今天是自2013/09/01日,开学以来的第 " + td.week + " 周,今天星期" + td.day);
                td = TodayInfo("2013/12/29");
                console.log("今天是自2013/12/29日,开学以来的第 " + td.week + " 周,今天星期" + td.day);
            })();
    </script>
    
    </body>
    
    </html>
    

    参考:https://www.cnblogs.com/mawuhen/p/4020757.html
    https://www.cnblogs.com/yangpeng-jingjing/p/7527635.html

    相关文章

      网友评论

          本文标题:JS计算指定日期是距今的第几周,星期几

          本文链接:https://www.haomeiwen.com/subject/kyczqktx.html