gpt4 book ai didi

Javascript : find first and last date of every month between two dates?

转载 作者:行者123 更新时间:2023-11-30 11:37:33 25 4
gpt4 key购买 nike

我有两个日期,例如 1/04/2017 - 30/06/2017

我想找到上述日期范围内每个月的第一个和最后一个日期。

像这样-

1/04/2017 - 30/04/20171/05/2017 - 31/05/20171/06/2017 - 30/06/2017

这是因为我想在 fullcalendar 中使用这个解决方案来处理重复发生的事件,这样我就可以在下面的代码中添加日期范围 -

batches.push({
id : item.id,
title : item.title.textCapitalize(),
start : item.start_time,
end : item.end_time,
dow : item.weekdays,
ranges :
[
{
start : '2017-04-01', //all of april
end : '2017-04-20',
},
{
start : '2017-05-01', //all of may
end : '2017-05-31',
},
{
start : '2017-06-01', //all of june
end : '2017-06-30',
},
],
backgroundColor : backgroundColor,
borderColor : backgroundColor
});

最佳答案

这是一个工作版本

const aDay = 24 * 60 * 60 * 1000;
const pad = num => ("0" + num).slice(-2);

const formatDate = d => d.getFullYear() + "-" + pad(d.getMonth() + 1) + "-" + pad(d.getDate());

const getRange = range => {
ranges = [];

console.log("start", range.start, "end", range.end)

for (var i = range.start.getTime(), end = range.end.getTime(); i <= end;) {
var first = new Date(i), last = new Date(first.getFullYear(), first.getMonth() + 1, 0); // last day of the month
ranges.push({
start: formatDate(first),
end: formatDate(last)
})
i = last.getTime() + aDay;
// if (!confirm(formatDate(new Date(i)))) break
}
return ranges;
}

let range = { // your range
start: new Date(2019, 0, 1, 15, 0, 0, 0), // remember month is 0 based
end: new Date(2020, 0, 1, 15, 0, 0, 0) // use 15:00 to help DST and Timzones yesterday
};
console.log(getRange(range))
range = { // your range
start: new Date(2019, 1, 28, 15, 0, 0, 0), // remember month is 0 based
end: new Date(2019, 4, 1, 15, 0, 0, 0) // use 15:00 to help DST and Timzones yesterday
};
console.log(getRange(range))

关于Javascript : find first and last date of every month between two dates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43817763/

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