js 獲取每月有幾周,根據年月周獲取該周從周一到周日的日期等方法


本文基於react-native

本人在用react-native寫一個關於課程表的APP時需要課程表按照日期周期顯示,網上查了許多方法,都沒有達到自己想要的效果,根據一些方法的參考,再根據自己思維寫出自己想要的效果如下圖所示

獲取每月有幾周(注:從第一個周一開始算該月第一周)

    getWeeks(year, month) {
        var d = new Date();
        // 該月第一天
        d.setFullYear(year, month-1, 1);
        var w1 = d.getDay();
        if (w1 == 0) w1 = 7;
        // 該月天數
        d.setFullYear(year, month, 0);
        var dd = d.getDate();
        // 第一個周一
        let d1;
        if (w1 != 1) d1 = 7 - w1 + 2;
        else d1 = 1;
        let week_count = Math.ceil((dd-d1+1)/7);
        return week_count;
    }

根據年月周獲取該周從周一到周日的日期

    getWeekTime(year, month,weekday) {
        var d = new Date();
        // 該月第一天
        d.setFullYear(year, month-1, 1);
        var w1 = d.getDay();
        if (w1 == 0) w1 = 7;
        // 該月天數
        d.setFullYear(year, month, 0);
        var dd = d.getDate();
        // 第一個周一
        let d1;
        if (w1 != 1) d1 = 7 - w1 + 2;
        else d1 = 1;
        var monday = d1+(weekday-1)*7;
        var sunday = monday + 6;
        var from = year+"-"+month+"-"+monday;
        var to;
        if (sunday <= dd) {
            to = year+"-"+month+"-"+sunday;
        } else {
            d.setFullYear(year, month-1, sunday);
            let days=d.getDate();
            to = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+days;
        }
        console.log(weekday+" 從" + from + " 到 " + to + "");
    }

獲取每月第幾周的周一的日期

    getMondayTime(year, month,weekday) {
        var d = new Date();
        // 該月第一天
        d.setFullYear(year, month-1, 1);
        var w1 = d.getDay();
        if (w1 == 0) w1 = 7;
        // 該月天數
        d.setFullYear(year, month, 0);
        var dd = d.getDate();
        // 第一個周一
        let d1;
        if (w1 != 1) d1 = 7 - w1 + 2;
        else d1 = 1;
        var monday = d1+(weekday-1)*7;
        return monday;
    }

獲取周一的日期

    getMonDate() {
        var d=new Date(),
            day=d.getDay(),
            date=d.getDate();
        if(day==1)
            return d;
        if(day==0)
            d.setDate(date-6);
        else
            d.setDate(date-day+1);
        return d;
    }

獲得周期名字

    getDayName(day) {
        var day=parseInt(day);
        if(isNaN(day) || day<0 || day>6)
            return false;
        var weekday=["周日","周一","周二","周三","周四","周五","周六"];
        return weekday[day];
    }

獲得當前日期在當月第幾周

    //不包括跟上個月重合的部分
    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
        );
    }

獲取當月最后一天日期

    getLastDay(year,month) {
        var new_year = year;    //取當前的年份
        var new_month = month++;//取下一個月的第一天,方便計算(最后一天不固定)
        if(month>12) {
            new_month -=12;        //月份減
            new_year++;            //年份增
        }
        var new_date = new Date(new_year,new_month,1);                //取當年當月中的第一天
        return (new Date(new_date.getTime()-1000*60*60*24)).getDate();//獲取當月最后一天日期
    }

根據當前日期獲取該天所在周,若該月1號不是周一,獲取該月第一周的周一日期,小於該日期的歸為上個月最后一周

    //當前日期幾月第幾周
    showDate(){
        var that=this;
        var d=this.getMonDate();
        var ds=new Date();
        var arr=[];
        for(var i=0; i<7; i++) {
            let weekDay=this.getDayName(d.getDay());
            let date=d.getDate()+'日';
            if(weekDay=='周一'){
                let beginTime=ds.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();
                that.state.beginTime = beginTime;
            }
            if(weekDay=='周日'){
                let endTime=ds.getFullYear()+'-'+(d.getMonth()+1)+'-'+d.getDate();
                that.state.endTime = endTime;
            }
            arr.push({weekDay:weekDay,date:date});
            d.setDate(d.getDate()+1);
        }
        let month=ds.getMonth()+1;
        let weeks=this.getMonthWeek(ds.getFullYear(),month,ds.getDate())-1;
        //每月周一日期
        let oneDate=that.getMondayTime(ds.getFullYear(),month,1);
        if(ds.getDate()<oneDate){
            month=ds.getMonth();
            weeks=this.getWeeks(ds.getFullYear(),month);
        }
        console.log('month:',month,'weeks:',weeks);
        this.setState({
            list:arr,
            yearDate:ds.getFullYear(),
            monthDate:month,
            month:ds.getMonth()+1,
            monthWeek:'第'+weeks+'周',
            theMonthWeek:'第'+weeks+'周'
        });
    }


免責聲明!

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



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