js获取最近7天的时间

1、首先获取当前年月日

 
//获取当前年月日
var now = new Date();
var year = now.getFullYear(); //得到年份
var month = now.getMonth()+1;//得到月份
var date = now.getDate();//得到日期
data = year + "年" + month + "月" + date + "日" ;

2、从从今往后的7天

var check=[];
for (let i = 0; i < 7; i++) {
	//24 * 3600 * 1000 就是计算一天的时间  
	var date = new Date(now.getTime() + i * 24 * 3600 * 1000);
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var day = date.getDate();
	var dt2 = new Date(now.getTime() + i * 24 * 3600 * 1000);
	var weekDay = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
	var weekDays = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
	
	// console.log(duty);
	//把七天的时间和星期添加到数组中
	
	check.push({
		date:year + "-" + month + "-" + day,
		day: day,
		weekday: weekDays[dt2.getDay()],
		week: weekDay[dt2.getDay()],
	});
	
}
// console.log(check);
t.week = check

4、获取7天的日期

GetTime() {
	var date = new Date();
	var base = Date.parse(date); // 转换为时间戳
	var year = date.getFullYear(); //获取当前年份
	var mon = date.getMonth() + 1; //获取当前月份
	var day = date.getDate(); //获取当前日
	var oneDay = 24 * 3600 *1000
		//var daytime = `${year}${mon >= 10 ? mon : '0' + mon}${day >= 10 ? day : '0' + day}`; //今日时间
		//this.$data.daytime = daytime; // 今日时间赋值给变量
				
	var daytimeArr = []
		for (var i = 1; i < 7 ; i++) { //前七天的时间
			var now = new Date(base -= oneDay);
			var myear = now.getFullYear();
			var month = now.getMonth() + 1;
			var mday = now.getDate()
			daytimeArr.push([myear, month >=10 ?month :'0'+ month, mday>=10?mday:'0'+mday].join('-'))
		}
			return daytimeArr
    }

你可能感兴趣的:(javascript,前端,开发语言)