/**
* 获取当前年月日以及前n天的年月日
* @data:[数据类型是int],指的是共几天
* 输出格式["2024-05-02", "2024-05-03"]
*/
getSomeDay(data) {
let nowDate = new Date(); //获取当前年月日
const year = nowDate.getFullYear(); //获取当前年份
let month = nowDate.getMonth() + 1;//月份
month = month < 10 ? "0" + month : month; //如果是单数在前面加0
const h = nowDate.getHours(); // 时
const m = nowDate.getMinutes(); // 分
const s = nowDate.getSeconds(); // 秒
nowDate.setDate(nowDate.getDate() - data + 1); //获取n天前的日期
let dateArray = [];
let dateTemp;
let flag = 1;
for (let i = 0; i < data; i++) {
let day = nowDate.getDate();//日
day = day < 10 ? "0" + day : day;
dateTemp = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s;
dateArray.push(dateTemp);
nowDate.setDate(nowDate.getDate() + flag);
}
return dateArray;
},
案例:获取前15天