获取接下来 3 个月的星期四并将日期输出到 select
Get the next 3 months of Thursdays and output the date into a select
我需要获取接下来 3 个月的星期四的日期,然后将这些日期输出到 select 输入中,值的格式为 dd/mm/yyyy 但选项显示为“星期四2021 年 4 月 12 日”,最好在 jQuery,但 PHP 也可以。
<select>
<option value="08/04/2021">Thursday 8th April 2021</option>
<option value="15/04/2021">Thursday 15th April 2021</option>
<option value="22/04/2021">Thursday 22nd April 2021</option>
etc
etc
</select>
我发现这个 jsfiddle 可以获取当月的所有星期四,但我需要它是从今天开始的接下来 3 个月的星期四。 (如果今天是星期四,则包括今天)。
如果是从今天开始的下一个 12 个星期四就可以了。
要获得接下来的 X 个月,您可以这样做:
function getNextMonths(num, include_current = false) {
let current = new Date();
let months = [];
if (include_current) {
months.push(current);
num--;
}
for (let i = 1; i <= num; i++) {
let next = new Date();
next.setDate(1);
next.setMonth(current.getMonth() + i);
months.push(next);
}
return months;
}
console.log(getNextMonths(3)); // Gives you the next three months
从那里开始,您只需要循环月份并评估他们的日子:
function getDayOfWeek(num_week_day, dates) {
let days = [];
for (let i = 0; i < dates.length; i++) {
// Evaluate current month
let current = {
year: dates[i].getFullYear(),
month: dates[i].getMonth()
};
current.days = new Date(current.year, current.month + 1, 0).getDate();
// Loop & evaluate days
for (let d = 1; d <= current.days; d++) {
let date = new Date(current.year, current.month, d);
if (date.getDay() == num_week_day) {
days.push(date);
}
}
}
return days;
}
// Get all Thursdays (4th day of the week) within the next 3 months.
console.log(getDayOfWeek(4, getNextMonths(3)));
// Get Thursdays within the next 3 months including the current one
console.log(getDayOfWeek(4, getNextMonths(3, true)));
// Get Thursdays within the next 3 months including the current one...
let thursdays = getDayOfWeek(4, getNextMonths(3, true));
//...but keep only those Thursdays that are in the future
let today = new Date();
let filtered = thursdays.filter(function (date) {
return date.getTime() >= today.getTime();
});
console.log(thursdays, filtered);
两个函数 return 一个 Date
对象的数组 - 您可能需要根据需要格式化这些对象。有关如何执行此操作的不同方法,请参阅此线程:
- How to format a JavaScript date
正如评论中已经指出的那样,通过引用 , you also might want to consider moment.js 进行 JavaScript 日期操作。
我需要获取接下来 3 个月的星期四的日期,然后将这些日期输出到 select 输入中,值的格式为 dd/mm/yyyy 但选项显示为“星期四2021 年 4 月 12 日”,最好在 jQuery,但 PHP 也可以。
<select>
<option value="08/04/2021">Thursday 8th April 2021</option>
<option value="15/04/2021">Thursday 15th April 2021</option>
<option value="22/04/2021">Thursday 22nd April 2021</option>
etc
etc
</select>
我发现这个 jsfiddle 可以获取当月的所有星期四,但我需要它是从今天开始的接下来 3 个月的星期四。 (如果今天是星期四,则包括今天)。
如果是从今天开始的下一个 12 个星期四就可以了。
要获得接下来的 X 个月,您可以这样做:
function getNextMonths(num, include_current = false) {
let current = new Date();
let months = [];
if (include_current) {
months.push(current);
num--;
}
for (let i = 1; i <= num; i++) {
let next = new Date();
next.setDate(1);
next.setMonth(current.getMonth() + i);
months.push(next);
}
return months;
}
console.log(getNextMonths(3)); // Gives you the next three months
从那里开始,您只需要循环月份并评估他们的日子:
function getDayOfWeek(num_week_day, dates) {
let days = [];
for (let i = 0; i < dates.length; i++) {
// Evaluate current month
let current = {
year: dates[i].getFullYear(),
month: dates[i].getMonth()
};
current.days = new Date(current.year, current.month + 1, 0).getDate();
// Loop & evaluate days
for (let d = 1; d <= current.days; d++) {
let date = new Date(current.year, current.month, d);
if (date.getDay() == num_week_day) {
days.push(date);
}
}
}
return days;
}
// Get all Thursdays (4th day of the week) within the next 3 months.
console.log(getDayOfWeek(4, getNextMonths(3)));
// Get Thursdays within the next 3 months including the current one
console.log(getDayOfWeek(4, getNextMonths(3, true)));
// Get Thursdays within the next 3 months including the current one...
let thursdays = getDayOfWeek(4, getNextMonths(3, true));
//...but keep only those Thursdays that are in the future
let today = new Date();
let filtered = thursdays.filter(function (date) {
return date.getTime() >= today.getTime();
});
console.log(thursdays, filtered);
两个函数 return 一个 Date
对象的数组 - 您可能需要根据需要格式化这些对象。有关如何执行此操作的不同方法,请参阅此线程:
- How to format a JavaScript date
正如评论中已经指出的那样,通过引用