//获取日期所在年的第几周
const getWeekInYear=(date)=>{
//判断该星期是否跨年,如果跨年就是第一周
let weekStartDate = getWeekStartByDate(date);//一周开始时间
var endDate = getDateFromDay(weekStartDate,6);//一周结束时间
if(weekStartDate.getFullYear()!=endDate.getFullYear()) return 1;
let d1 = new Date(date);
let d2 = new Date(date);
d2.setMonth(0);
d2.setDate(1);
d2 = getWeekStartByDate(d2)
let rq = d1-d2;
let days = Math.ceil(rq/(24*60*60*1000))+1;
let num = Math.ceil(days/7);
//console.log('第'+num + '周')
return num;
}
//根据传入的日期查找周的开始日期,开始日期为周日,固定
const getWeekStartByDate = (date) =>{
let day = date.getDay();
return getDateFromDay(date,-day);
}
function getDateFromDay(dayDate , day){
let date = new Date();
date.setTime(dayDate.getTime() + day * 24 * 60 * 60 * 1000);
return date;
}
网友评论