我想要一个程序,可以生成每个星期的第一个日期,这样我就可以把它插入到下面的代码
const dateOfTheFirstDay = null;
//preferred format: "April 9, 2023 00:00:01"
const d = new Date(dateOfTheFirstDay);
setNewWeekTime(d.getTime());
从上面的代码中,我可以得到1970年1月1日00:00到一周的第一个日期之间的毫秒数。
由于一周中任何给定的一天都保证间隔7天,您实际上只需要找到一个初始参考日期,然后向前或向后增加7的倍数。请记住,您需要决定如何处理时区问题。
这是一个基于生成器的解决方案,将产生从1583年开始的任何给定日期(为了避免采用公历之前的日期怪异,请参阅预言公历)。
function* dayGenerator(day) {
const addDays = (date, days) => date.setUTCDate(date.getUTCDate() + days);
const ref = new Date(Date.UTC(1583, 0, 1, 12, 0, 0));
while (ref.toLocaleString('en-us', { weekday: 'long' }) !== day) {
addDays(ref, 1);
}
yield new Date(ref);
while (true) {
yield new Date(addDays(ref, 7));
}
}
/* Example output */
// The first 10,000 Sundays from Jan 1, 1583
console.log('Sundays:');
const sundayGenerator = dayGenerator('Sunday');
for (let i = 0; i < 10000; i++) {
const next = sundayGenerator.next().value;
if (i <= 3 || i === 9999) {
console.log(
`Sunday ${i + 1}:`,
next,
next.toLocaleString('en-us', { weekday: 'long' })
);
}
}
// A few Tuesdays
console.log('Tuesdays:');
const tuesdayGenerator = dayGenerator('Tuesday');
for (let i = 0; i < 3; i++) {
const next = tuesdayGenerator.next().value;
console.log(next, next.toLocaleString('en-us', { weekday: 'long' }));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
.as-console-row::after { display: none !important; }
您可以通过接受startDate
和endDate
使其通用,并且可以进一步自定义如果start > end
则向后递增。这里使用一种基于替代算法的方法来确定在传递的开始日期之后的第一个匹配日。
function* dayGenerator(
day,
startDateISO = '1583-01-01T12:00:00.000Z',
endDateISO = '9999-12-31T12:00:00.000Z'
) {
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',];
const ref = new Date(startDateISO);
ref.setDate(ref.getDate() + ((7 + days.indexOf(day) - ref.getDay()) % 7));
yield new Date(ref);
const endDate = new Date(endDateISO);
while (ref < endDate) {
yield new Date(ref.setDate(ref.getDate() + 7));
}
}
// Spread the full output of the iterator into an array
const sundays2023 = [
...dayGenerator('Sunday', '2023-01-01T12:00:00Z', '2023-12-31T12:00:00Z'),
];
console.log(
'First: ',
sundays2023[0],
sundays2023[0].toLocaleString('en-us', { weekday: 'long' })
);
console.log(
'Last: ',
sundays2023.at(-1),
sundays2023.at(-1).toLocaleString('en-us', { weekday: 'long' })
);
(奇怪的是2023开始和结束都是星期天)
获取两个日期之间的星期日数组的有效算法是获取参考日期上或之前的星期日,然后依次添加7天,直到到达结束日期。
以下命令获取所提供日期所在年份的所有星期日,并将当前日期(以及年份)作为默认值。
。
// Return an array of all Sundays in year of
// supplied date
function getSundaysInYear(date = new Date()){
// Get first Sunday of year
let year = date.getFullYear();
let start = new Date(year, 0, 7);
start.setDate(start.getDate() - start.getDay());
// Set last date of year
let end = new Date(year, 11, 31);
// Array of Sundays
let sundays = [];
while (start <= end) {
sundays.push(new Date(start));
start.setDate(start.getDate() + 7);
}
return sundays
}
// Example
let d = new Date()
let sundays = getSundaysInYear(d);
console.log(`${d.getFullYear()} has ${sundays.length} Sundays.` +
`nFirst: ${sundays[0].toString().substr(0,10)} ` +
`nLast : ${sundays[sundays.length - 1].toString().substr(0,10)}`
);
下面是在javascript中实现请求的代码
const startDate = new Date('January 1, 2023');
const endDate = new Date('December 31, 2023');
const days = ['Sunday'];
const sundayDates = [];
for (let date = startDate; date <= endDate; date.setDate(date.getDate() + 1)) {
if (days.includes(date.toLocaleString('en-us', { weekday: 'long' }))) {
sundayDates.push(new Date(date));
}
}
sundayDates
数组将给出您希望的日期