gpt4 book ai didi

javascript - 如何使用 javascript 查找每个月的周/工作日的相同组合

转载 作者:行者123 更新时间:2023-11-28 07:42:08 25 4
gpt4 key购买 nike

我正在尝试计算日期范围内的重复日期。我粘贴下面的代码,该代码仅与我的代码中的重复部分相关。它没有计算每个月同一天或任何模式的逻辑。它只有一个间隔。请提出一个解决方案,从 Between[] 数组中找出每个月的同一天,该数组已经包含开始日期和结束日期之间的范围。

switch (true) {
case (interval == 7):
//weekly
//Push in the selected dates in the selected array.
for (var i = 0; i < between.length; i += 7) {
selected.push(between[i]);
}
break;
case (interval == 30):
//Monthly
//Push in the selected dates in the selected array.
for (var i = 0; i < between.length; i += 30) {
selected.push(between[i]);
}
break;
case (interval == 15):
//Bi-Monthly
//Push in the selected dates in the selected array.
for (var i = 1; i < between.length; i += 15) {
selected.push(between[i]);
}
break;
case (interval == 0):
//One time event
//Push in the selected dates in the selected array.
for (var i = 0; i < 1; i++) {
selected.push(between[i]);
}
break;
default:
return undefined;
}

查找下一个重复日期的标准应如下所示:

For example a user selects Wednesday from the first week of January then the next recurring date should be the Wednesday from the first week of February.

最佳答案

首先定义选定的Date对象。

var between = [new Date(2015, 0, 11), new Date(2015, 0, 20)]

现在确定我们需要的组件。它们是:下一次约会应该什么时候开始。

var start = new Date(between[0].getFullYear(),between[0].getMonth(),between[0].getDate()+27)

它将在下个月的开始日期和您所描述的同一“日期”得出结果。

将其包装为函数:

var getNextMonth = function (date, howmany){
return new Date(date.getFullYear(), date.getMonth(), date.getDate()+28*howmany);
};

var getNextMonthRange = function (between, howmany){
return [getNextMonth(between[0], howmany), getNextMonth(between[1], howmany)]
};

示例

var between = [new Date(2015, 0, 11), new Date(2015, 0, 20)]; // Sunday, 11 Jan 2015 to Tuesday, 20 Jan 2015
var nextBetween = getNextMonthRange(between, 1); // Sunday, 8 Feb 2015 to Tuesday, 17 Feb 2015

我希望这就是你想要的:D

关于javascript - 如何使用 javascript 查找每个月的周/工作日的相同组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27883478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com