计算日期间隔内的日期
我有一个包含日期和时间的数组 abc
。我需要将其转换为 1 天的时间段,其限制由 startDate
和 endDate
确定,其中“x”包含时间段,“y”包含这些时间段内的发生次数插槽。如何根据间隔获取 abc
中出现的次数,并根据日期间隔正确映射它?
const abc = ['2021-09-05T00:53:44.953Z', '2021-08-05T05:08:10.950Z', '2022-03-05T00:53:40.951Z'];
const startDate = '2021-07-05';
const endDate = '2021-11-05';
const res = [{x: '2021-07-05 - 2021-08-05' , y: '1' },{x: '2021-08-05 - 2021-09-05' , y: '2' }, {x: '2021-09-05 - 2021-10-05' , y: '1' },{x: '2021-10-05 - 2021-11-05' , y: '0' }];
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I've an array abc
containing date and time. I need to convert it into time slots of 1 day with limits determined by startDate
and endDate
with 'x' containing time slots and 'y' containing count of occurrence in those time slots. How can I get the count of occurrences in abc
as per the interval and map it correctly it as per the date intervals?
const abc = ['2021-09-05T00:53:44.953Z', '2021-08-05T05:08:10.950Z', '2022-03-05T00:53:40.951Z'];
const startDate = '2021-07-05';
const endDate = '2021-11-05';
const res = [{x: '2021-07-05 - 2021-08-05' , y: '1' },{x: '2021-08-05 - 2021-09-05' , y: '2' }, {x: '2021-09-05 - 2021-10-05' , y: '1' },{x: '2021-10-05 - 2021-11-05' , y: '0' }];
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的理解,我根据您在问题中提供的
开始
和结束
日期创建了一个简单的工作演示:注意:我假设您必须在
startDate
和endDate
之间一次获取一个范围。As per my understanding, I created a simple working demo as per the
start
andend
date you provided in the question :Note : I am assuming you have to fetch a range once at a time between
startDate
andendDate
.这可能是实现预期目标的一种可能的解决方案:
代码片段
请查看
countWithinLimits
方法,它包含解决方案的重要部分。解释
虽然上面的代码片段中有内嵌注释,但如果有任何具体点需要更详细的解释,下面的“注释”可能会被用来通知,并且这个答案可能会被更新有更多细节。
总体思路是这样的:
2021-07-05 - 2021-07-06
)[ {x : '2021-07-05 - 2021-07-06', y: '2' }, .... ]
)This may be one possible solution to achieve the desired objective:
Code Snippet
Please look at
countWithinLimits
method which houses the significant portions of the solution.Explanation
While there are comments in-line in the above code-snippet, should there be any specific point that requires a more detailed explanation, 'comments' below may be used to notify and this answer may be updated with more details.
The big-picture idea is this:
2021-07-05 - 2021-07-06
)[ {x : '2021-07-05 - 2021-07-06', y: '2' }, .... ]
)