我愿意根據“每月的第 n 個作業日”創建一個回圈日期 這是我選擇當前日期時的解決方案作業
let dates = {};
const currentDate = dayjs();
const recurrence = currentDate
.recur(dayjs().add(4, "month"))
.every("Wednesday")
.daysOfWeek()
.every(currentDate.monthWeekByDay())
.weeksOfMonthByDay();
recurrence.all().forEach((date) => {
dates[date.format("YYYY-MM-DD")] = { selected: true, };
});
// dates = ["2022-09-21","2022-10-19","2022-11-16","2022-12-21","2023-01-18"]
但是如果把這個月的最后一天是 30
let dates = {};
const lastDayofMonth = dayjs().endOf("month");
const recurrence = lastDayofMonth
.recur(dayjs().add(4, "month"))
.every("Friday")
.daysOfWeek()
.every(lastDayofMonth.monthWeekByDay())
.weeksOfMonthByDay();
我期待得到
["2022-09-30","2022-10-28","2022-11-25","2022-12-30"..]
代替
["2022-09-30","2022-12-30"]
這是演示
我錯過了什么嗎?提前致謝
uj5u.com熱心網友回復:
可以使用dayjs和dayjs-recur創建基于“每月第 n 個作業日”的重復日期,所有月份為不同的周數
但也有一些挑戰:
- 一個月平均有 4 周(有些月的第五周很少或沒有天)
- 因此,可能會出現沒有第五周或第五周沒有第 n 個作業日的情況
解決這個問題
- 如果您獲得一個月的第 n 個作業日,該周是該月的最后一周(可能是第 4 周/第 5 周)
- 獲取最后一周(第 5 周)和第四周(第 4 周)的每月第 n 個作業日
- 如果一個月的日期出現多次,則從回傳的日期串列中篩選并選擇最新/最大的一個
- 過濾后的結果應該是每月的第 n 個作業日,其中該周是該月的最后一周
演示
下面是從@Achraf 的代碼沙箱演示中派生的作業演示的鏈接
演示鏈接
代碼示例
const lastDayofMonth = dayjs().endOf("month");
const recurrence = lastDayofMonth
.recur(dayjs().add(4, "month"))
.every("Friday")
.daysOfWeek()
.every([3, 4])
.weeksOfMonthByDay();
// filter recurrence
const months = {};
recurrence.all().forEach((date) => {
months[date.month()] = date;
})
// get filtered dates
const filteredDates = Object.values(months);
// formot date into dates object
filteredDates.forEach((date) => {
dates[date.format("YYYY-MM-DD")] = { selected: true, color: "#2FB0ED" };
});
console.log("dates", dates);
// ["2022-09-30","2022-10-28","2022-11-25","2022-12-30"..]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/510335.html
