vue3 获取当前日期及前7天或前N天的日期

let date = new Date();
let frontDate = new Date() 
let year = date.getFullYear(); 
frontDate.setDate(frontDate.getDate() - 7)//前7天减7,前N天减N
let dateArr1 = [frontDate.getMonth() + 1,frontDate.getDate()];
let dateArr2 = [date.getMonth() + 1,date.getDate()];
//开始格式是M,经过以下循环变为MM
for (let i = 0; i < dateArr1.length; i++) {
   if (dateArr1[i] >= 1 && dateArr1[i] <= 9) {
        dateArr1[i] = "0" + dateArr1[i];
    }
}
for (let i = 0; i < dateArr2.length; i++) {
    if (dateArr2[i] >= 1 && dateArr2[i] <= 9) {
        dateArr2[i] = "0" + dateArr2[i];
    }
}
let strDate1 =year +"/" + dateArr1[0] +"/" +dateArr1[1] +" " 
let strDate2 =year +"/" + dateArr2[0] +"/" +dateArr2[1] +" "
let time_list = [strDate1,strDate2]  //[2023-08-31,2023-09-07]

代码未进行优化,根据自己需要进行修改~

你可能感兴趣的:(vue3随手记,前端)