js gets all Mondays and Sundays of the current month

js gets all Mondays and Sundays of the current month

 			//一共几个周一
			 getMonthWeek(year, month) {//由于这里我以获取周一为例所以向上取整
				 var d = new Date(year, month, 0);
				 var t = Math.ceil(d.getDate()/7)
				    return t;
			},
			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;
			},
			gettime(){
				  var ds=new Date();
				
				  let month=ds.getMonth()+1;//当前月
				  let weeks=this.getMonthWeek(ds.getFullYear(),month);//调用方法传入当前年和当前月获得本月几周
				  //获取月第一个周一日期
				  let oneDate=this.getMondayTime(ds.getFullYear(),month,2);
				  var weeksall = [] //所有周一
				    for(var i = 0 ;i<weeks;i++){
				      weeksall.push(oneDate+i*7)
				    }
				console.log(weeksall)
			},

I take the example of getting all Mondays of each month, which can be expanded on the basis

Guess you like

Origin blog.csdn.net/Fairyasd/article/details/113727887