js获取当前时间前后7天,存入数组

const temp = [];
for (let i = 0; i < 15; i++) {
    const time = new Date(new Date().setDate((new Date().getDate() + i) - 7));
    const year = time.getFullYear();
    const month = `0${time.getMonth() + 1}`.slice(-2);
    const strDate = `0${time.getDate()}`.slice(-2);
    temp.push(`${year}-${month}-${strDate}`)
}
console.log(temp);

你可能感兴趣的:(js获取当前时间前后7天,存入数组)