Bootstrap

js获取当前/上周是今年的第几周

otherDay() {
	let today = moment().format('YYYY-MM-DD')
	let last_month = moment().subtract(1,'month').format('YYYY-MM-DD')
    let newYearsDay = moment().startOf('year').format('YYYY-MM-DD')
    let 
  	//new Date(today.substr(0, 4), 0, 1)是今年第一天

    let lastMouth = 'M'+today.substr(0, 4)+last_month.substr(5, 2)
    let threeWeeksAgo = 'W'+newYearsDay.substr(0, 4)+(diffW-2)
    let twoWeeksAgo = 'W'+newYearsDay.substr(0, 4)+(diffW-1)
    let lastWeek = 'W'+newYearsDay.substr(0, 4)+diffW
                
    return [lastMouth, threeWeeksAgo, twoWeeksAgo, lastWeek, this.threeDays_ago, this.twoDays_ago, this.yesterday]                
},
diffD = moment(today).diff(moment(newYearsDay),'days')  // today和今年第一天的自然天差值
// 上周是今年的第diffW周
var diffW = Math.ceil((diffD + ((new Date(today.substr(0, 4), 0, 1).getDay() + 1) - 1)) / 7)  
// let diffW = moment(today).diff(moment(newYearsDay),'weeks') // today和今年第一天的自然周差值


var getYearWeek = function(a, b, c){
	// date1是当前日期 date2是当年第一天 d是当前日期是今年第多少天
	// 用d + 当前年的第一天的周差距的和在除以7就是本年第几周
	var date1 = new Date(a, parseInt(b) - 1, c),
    	date = new Date(),
    	date2 = new Date(a, 0, 1),
    	d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
	return Math.ceil((d + ((date2.getDay() + 1) - 1)) / 7);
};
var diffW=getYearWeek(today.substr(0, 4), today.substr(5, 2), today.substr(8, 2)); // 上周是今年的第几周

var diffW1 = Math.ceil((diffD + ((new Date(today.substr(0, 4), 0, 1).getDay() + 1) - 1)) / 7)  //new Date(today.substr(0, 4), 0, 1)是今年第一天

;