js 獲取年、月、周、當前日期第幾周、這月有那幾周


查看當前日期是第幾周:https://wannianli.tianqi.com/today/zhou/

//獲取完整的日期 

var date=new Date;

var y = date.getFullYear()
var m = date.getMonth() + 1

var temptime="";

temptime= new Date(y - 2, m, d)   // 往前一年

temptime= new Date(y , m+4-1, d)  //往后4個月 
獲取當前第幾周

        function theWeek() {
            var totalDays = 0;
            now = new Date();
            years = now.getYear()
            if (years < 1000)
                years += 1900
            var days = new Array(12);
            days[0] = 31;
            days[2] = 31;
            days[3] = 30;
            days[4] = 31;
            days[5] = 30;
            days[6] = 31;
            days[7] = 31;
            days[8] = 30;
            days[9] = 31;
            days[10] = 30;
            days[11] = 31;
            //判斷是否為閏年,針對2月的天數進行計算
            if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
                days[1] = 29
            } else {
                days[1] = 28
            }
            if (now.getMonth() == 0) {
                totalDays = totalDays + now.getDate();
            } else {
                var curMonth = now.getMonth();
                for (var count = 1; count <= curMonth; count++) {
                    totalDays = totalDays + days[count - 1];
                }
                totalDays = totalDays + now.getDate();
            }
            //得到第幾周
            var week = Math.round(totalDays / 7);
            return week;
        }
獲取當月有那幾周

        function GetCustomWeekList(year, month) {
            var rtn_Weeks = [];

            // 獲取當月屬於當年的第幾周
            function getWeek(dt) {
                let d1 = new Date(dt);
                let d2 = new Date(dt);
                d2.setMonth(0);
                d2.setDate(1);
                let rq = d1 - d2;
                let days = Math.ceil(rq / (24 * 60 * 60 * 1000));
                let num = Math.ceil(days / 7);
                return num;
            }
            //獲取當前月有幾周
            function getMonthWeek(a, b, c) {
                var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate();
                return Math.ceil(
                    (d + 6 - w) / 7
                );
            };
            var last = new Date(year, month, 0);//獲取當前月最后一天時間
            var y = last.getYear();
            var m = last.getMonth() + 1;
            var d = last.getDate();

            var ss = getWeek(year + "-" + month + "-1")
            for (var i = 0; i < getMonthWeek(y, m, d); i++) {
                item = [];
                item.push(ss + i);
                rtn_Weeks.push(item);
            }
            return rtn_Weeks;
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM