我正在尝试获取特定时段内每个星期一的日期时间范围,并且能够从本月到年末使用,但是我可以动态设置时段。
This is my code
enum DAYS {
MON = 1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
function getDays(day) {
const d = new Date(new Date().setHours(0, 0, 0, 0)),
month = d.getMonth(),
days = [];
d.setDate(day);
while (d.getDay() !== day) {
console.log('d')
d.setDate(d.getDate() + 1);
}
while (d.getMonth()) {
const date = new Date(d.getTime())
const start = date.getTime()
const end = start + 24 * 60 * 60
const range = {
date,
start,
end
}
days.push(range);
d.setDate(d.getDate() + 7);
}
return days;
}
const Mondays = getDays(DAYS.MON)
const Tuesdays = getDays(DAYS.TUE)
console.log(Mondays)
console.log(Tuesdays)
例如,如果我想让整个星期一从14/06/2019到13/06/2020,我如何将期间设置为开始日期和结束日期? 你能不能请别人给我建议?
答案 0 :(得分:0)
enum DAYS {
MON = 1,
TUE,
WED,
THU,
FRI,
SAT,
SUN
}
function getDays(day, startDate, endDate) {
const d = new Date(startDate)
const days = [];
d.setDate(day);
while (d.getDay() !== day) {
d.setDate(d.getDate() + 1);
}
while (d < endDate) {
if (d >= startDate) {
const date = new Date(d.getTime())
const start = date.getTime()
const end = start + 24 * 60 * 60
const range = {
date,
start,
end
}
days.push(range);
}
d.setDate(d.getDate() + 7);
}
return days;
}
const startDate = new Date('Thu Jan 20 2020 00:00:00 GMT+0900 (Japan Standard Time)')
const endDate = new Date('Thu Jun 19 2022 00:00:00 GMT+0900 (Japan Standard Time)')
const day = DAYS.MON
const Mondays = getDays(day, startDate, endDate)