我怎么能从今天知道月份和前一天



现在是28 jul,我想在当前日期之前7天

let daysWithMonth = []
for (let i = 1; i < 31; i++) {
const d = new Date();
const day = new Date(d.setDate(d.getDate() - i)).getDate()
if(i%5==0){
daysWithMonth.push(day)
}
}
console.log(daysWithMonth)

但在我的名单中,我想知道更多的月份,比如今年7月7日,6月28日

let currentDate = new Date();
let prevDate = currentDate;
let prevDates = [];
let daysBefore = 7;
let options = { year: 'numeric', month: 'long', day: 'numeric' }
for(let i=1;i <= daysBefore;i++){
prevDate = new Date(prevDate - (24 * 60 * 60 * 1000))
prevDates.push(prevDate.toLocaleDateString(undefined,options))
} 
console.log(currentDate.getDate()) //Current Date
console.log(prevDates) //Previous 7 date

最新更新