前端如何获取近7天,近1年的日期进行查询?
methods:{
getRangeDate(ranges) {
let nowDays = new Date();
let getYear = nowDays.getFullYear();
let getMonth = nowDays.getMonth() + 1;
let getDate = nowDays.getDate();
let nd = new Date();
nd = nd.valueOf();
nd = nd - ranges * 24 * 60 * 60 * 1000;
nd = new Date(nd);
let lastYear = nd.getFullYear();
let lastMonth = nd.getMonth() + 1;
let lastDate = nd.getDate();
if (getMonth === 0) {
getMonth = '12';
getYear = new Date().getFullYear() - 1;
} else {
getYear = new Date().getFullYear();
}
if (getMonth < 10) {
getMonth = '0' + getMonth;
}
if (getDate < 10) {
getDate = '0' + getDate;
}
if (lastMonth < 10) {
lastMonth = '0' + lastMonth;
}
if (lastDate < 10) {
lastDate = '0' + lastDate;
}
let nowTime = getYear + '' + getMonth + '' + getDate;
console.log('当前日期', getYear, getMonth, getDate);
let lastTime = '';
lastTime = lastYear + '' + lastMonth + '' + lastDate;
console.log('近7天', lastTime);
let doubleTime = {
nowTime: nowTime,
lastTime: lastTime,
};
return doubleTime;
},
isOneYear() {
let year = new Date().getFullYear();
let lastYear = new Date().getFullYear() - 1;
let month = new Date().getMonth() + 1;
let getDate = new Date().getDate();
if (month < 10) {
month = '0' + month;
}
if (getDate < 10) {
getDate = '0' + getDate;
}
const nowDate =
year.toString() + '/' + month.toString() + '/' + getDate.toString();
let lastDate =
lastYear.toString() + '/' + month.toString() + '/' + getDate.toString();
console.log('isOneYear', nowDate, lastDate);
let nowTime = new Date(nowDate).getTime();
let lastTime = new Date(lastDate).getTime();
let doubleTime = {
nowTime: nowTime,
lastTime: lastTime,
};
return doubleTime;
},
}
created(){
this.getRangeDate(7);
this.isOneYear();
}