问答 百科手机端

Js 计算某日是本月、本年的第几周

2023-03-11 13:20

1、某日是本月第几周:
var mydate=new Date(theyear,themonth-1,theday);
var weekday=mydate.getDay(); //获取该日是星期几,0代表星期日
var weekno=Math.ceil((theday + 6 - weekday) / 7); // 本月第几周
2、本月共有几周:
//先获取本月天数 mydate.setMonth(nmonth); //移到下个月 mydate.setDate(0); //移回到本月最后一天 var totaldays=mydate.getDate();//本月天数 var thelastweekday=mydate.getDay();//最后一天星期几
var weeknum=Math.ceil((totaldays + 6 -thelastweekday) / 7); //本月共有几周

3、计算某日a年b月c日处于本年的第几周
var getYearWeek=function(a,b,c){
	var date1=new Date(a,b-1,c); //本日
	var date2=new Date(a,0,1); //本年元旦
	var d=Math.round((date1.valueOf()-date2.valueOf())/86400000); 
	return Math.floor((d+date2.getDay())/7)+1;
}; 

热门