js获取当前日期所在的周一到周日的日期,以及星期几

如图:


        $(function () {
            var now = new Date("2020-1-1");
            var nowTime = now.getTime() ;
            var day = now.getDay() || 7;
            var oneDayLong = 24*60*60*1000 ;

            var MondayTime = nowTime - (day-1)*oneDayLong  ;
            var SundayTime =  nowTime + (7-day)*oneDayLong ;

            var monday = new Date(MondayTime);

            for (let i = 0; i<7;i++){
                var tmpDate = new Date(MondayTime);
                tmpDate=tmpDate.setDate(tmpDate.getDate()+i);
                tmpDate=new Date(tmpDate);
                console.log(tmpDate);

                var mondayDateStr = (tmpDate.getMonth()+1)+'月';
                mondayDateStr += tmpDate.getDate()+'日';

                let days = tmpDate.getDay();
                switch (days) {
                    case 1:
                        days = '星期一';
                        break;
                    case 2:
                        days = '星期二';
                        break;
                    case 3:
                        days = '星期三';
                        break;
                    case 4:
                        days = '星期四';
                        break;
                    case 5:
                        days = '星期五';
                        break;
                    case 6:
                        days = '星期六';
                        break;
                    case 0:
                        days = '星期日';
                        break;
                }
                mondayDateStr += days;
                console.log(mondayDateStr) ;
            }


            var sunday = new Date(SundayTime);
            console.log(monday) ;
            console.log(sunday) ;
        });

 

你可能感兴趣的:(js,前端)