如何在 momentJS 中的两个日期对象之间获取所有特定的 weekDays
How to get all particular weekDays between two date objects in momentJS
如何获取开始日期和结束日期之间的所有工作日(相关的特定工作日)。该函数将采用 startDate、endDate(均作为时刻对象)、dayNum(作为整数)(0 - Sun,1 - Mon ... 6 - Sat)和 return 这些天的时刻对象列表。
我写了一个函数,但它不起作用。
const getDates = (startDate, endDate, dayNum) => {
let realStart = moment(startDate);
let end = moment(endDate);
let start = realStart.clone();
start.add(-15, "days");
let result = [];
var current = start.clone();
current.day(dayNum + 1);
while (current.isSameOrBefore(end)) {
if (current.isSameOrAfter(realStart)) {
let temp = current.clone();
result.push(temp);
current.day(8 + dayNum);
} else {
current.day(8 + dayNum);
}
}
return result;
};
我认为类似这样的东西对你想要做的事情有用:
const getDates = (startDate, endDate, dayNum) => {
let current = moment(startDate)
const end = moment(endDate)
const results = []
while(current.isSameOrBefore(end)) {
if (current.weekday() === dayNum) results.push(current.clone())
current.add(1, 'day')
}
return results
}
rzr_f的答案总是一天一天递增,当startDate
和endDate
之间的跨度较大,或者函数被重复调用时效率低下.
这是一个更高效的版本,每次增加 7 天,并且永远不必检查它添加的日期是否在正确的工作日,因为它总是从正确的工作日开始:
const getDates = (startDate, endDate, dayNum) => {
let current = moment(startDate)
const end = moment(endDate)
const results = []
// If current is on the wrong weekday, move it forward to the first matching weekday:
if (current.weekday() !== dayNum) {
current.add(dayNum >= current.weekday() ? ( dayNum - current.weekday() ) : ( 7 - ( current.weekday() - dayNum ) ), 'day')
}
while(current.isSameOrBefore(end)) {
results.push(current.clone())
current.add(7, 'day')
}
return results
}
如何获取开始日期和结束日期之间的所有工作日(相关的特定工作日)。该函数将采用 startDate、endDate(均作为时刻对象)、dayNum(作为整数)(0 - Sun,1 - Mon ... 6 - Sat)和 return 这些天的时刻对象列表。
我写了一个函数,但它不起作用。
const getDates = (startDate, endDate, dayNum) => {
let realStart = moment(startDate);
let end = moment(endDate);
let start = realStart.clone();
start.add(-15, "days");
let result = [];
var current = start.clone();
current.day(dayNum + 1);
while (current.isSameOrBefore(end)) {
if (current.isSameOrAfter(realStart)) {
let temp = current.clone();
result.push(temp);
current.day(8 + dayNum);
} else {
current.day(8 + dayNum);
}
}
return result;
};
我认为类似这样的东西对你想要做的事情有用:
const getDates = (startDate, endDate, dayNum) => {
let current = moment(startDate)
const end = moment(endDate)
const results = []
while(current.isSameOrBefore(end)) {
if (current.weekday() === dayNum) results.push(current.clone())
current.add(1, 'day')
}
return results
}
rzr_f的答案总是一天一天递增,当startDate
和endDate
之间的跨度较大,或者函数被重复调用时效率低下.
这是一个更高效的版本,每次增加 7 天,并且永远不必检查它添加的日期是否在正确的工作日,因为它总是从正确的工作日开始:
const getDates = (startDate, endDate, dayNum) => {
let current = moment(startDate)
const end = moment(endDate)
const results = []
// If current is on the wrong weekday, move it forward to the first matching weekday:
if (current.weekday() !== dayNum) {
current.add(dayNum >= current.weekday() ? ( dayNum - current.weekday() ) : ( 7 - ( current.weekday() - dayNum ) ), 'day')
}
while(current.isSameOrBefore(end)) {
results.push(current.clone())
current.add(7, 'day')
}
return results
}