如何在javascript中获取从今天到周日的日期



我试图有一个功能,显示从今天到星期日的日期。我的函数是这样的:

dateSets() {
const currentDate = new Date();
const today = new Date();
const sunday = new Date(
currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 7)
);
const dates = [];
for (let i = today.getDate(); i <= sunday.getDate(); i++) {
dates.push(today);
today.setDate(today.getDate() + 1);
}
console.log(dates);
const tomorrow = new Date(today);
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(tomorrow);
}

但是输出是这样的:

[Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time),
Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time),
Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time),
Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time),
Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time),
Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time),
Mon Aug 09 2021 14: 16: 38 GMT + 0200(Central European Summer Time)]
Table.vue:203 Tue Aug 10 2021 14:16:38 GMT+0200 (Central European Summer Time)

所以,它应该显示从今天,即8月2日,但它显示错误。

谢谢。

重构你的代码:

function dateSets() {
const today = new Date();
const sunday = new Date();
sunday.setDate(sunday.getDate() - sunday.getDay() + 7);
const dates = [];
if (today.getDay() === 0) {
dates.push(new Date(+today));
} else {
while (sunday >= today) {
dates.push(new Date(+today));
today.setDate(today.getDate() + 1);
}
}
return dates;
}
console.log(dateSets());
.as-console-wrapper { min-height: 100%;}

当你这样做的时候:

dates.push(today);

引用totoday推入dates数组中,因此数组中的所有引用都指向相同的日期。今天你所需要做的就是复制并将其推入数组:

dates.push(new Date(+today));

也:

for (let i = today.getDate(); i <= sunday.getDate(); i++)
星期日

在下一个月时不起作用,例如,如果今天是星期一8月30日21日,而星期日是星期日9月5日,那么测试在第一次迭代时失败,您将得到一个空的日期数组。

与几个优化:

function dateSets(date = new Date()) { // Allow passing a date
let currentDate = new Date(+date);
currentDate.setHours(0,0,0,0); // for convenience
let today = new Date(+currentDate);
let sunday = new Date(
currentDate.setDate(currentDate.getDate() - currentDate.getDay() + 7)
);
let dates = [];
while (sunday >= today) {       // While is simpler in this case
dates.push(new Date(+today)); // Push copies into the array
today.setDate(today.getDate() + 1);
}
return dates;
}
// Check ove month end
console.log(
dateSets(new Date(2021,7,30)).map(d=>d.toDateString())
);

您可以通过查找基于当前星期的偏移量并设置开始日期n-之前的天数来计算当前星期的日期。

编辑:我加上了"星期几"。偏移量,以便您可以从一周的当前日期开始获取日期。

const DAY_IN_MILLIS = 864e5;
const defaultOptions = {
weekdayOffset: 0,
startOfWeek: false,
includeSunday: false
};
const getDatesForCurrentWeek = (date, options) => {
const
result = [],
opts = { ...defaultOptions, ...options },
offset = (options.startOfWeek ? date.getDay() : opts.weekdayOffset) * DAY_IN_MILLIS;
let startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate());
startDate.setTime(startDate.getTime() - offset);
const limit = opts.includeSunday ? 8 : 7;
for (let dayOfWeek = opts.weekdayOffset; dayOfWeek < limit; dayOfWeek++) {
result.push(new Date(startDate.getTime() + (dayOfWeek * DAY_IN_MILLIS)));
}
return result;
};
const today = new Date();
console.log(`Day of week: ${today.toLocaleDateString('en-US', {
weekday: 'long'
})}`);
// Today to Sunday
console.log(getDatesForCurrentWeek(today, {
weekdayOffset: today.getDay(),
includeSunday: true
}));
// All dates for current week
console.log(getDatesForCurrentWeek(today, {
startOfWeek: true
}));
.as-console-wrapper { top: 0; max-height: 100% !important; }

最新更新