gpt4 book ai didi

javascript - 如何将时刻日期从时隙拆分为数组?

转载 作者:行者123 更新时间:2023-12-04 01:42:54 25 4
gpt4 key购买 nike

我有一个 startDateendDate我想拆分为 startDate 的数组和 endDate ?我的拆分器是一个时隙(不是分页中的数字)。

我的 typescript 代码是:

startDate: Date;
endDate: Date;
timeSlots: moment.Duration = moment.duration(30, 'minutes');

我的开始日期是 2019-11-02T11:57:00.701Z和我的 endDate 2019-11-02T13:31:00.701Z
我正在寻找如何 slpit 返回此结果:
[
{startDate: 2019-11-02T11:57:00.701Z, endDate: 2019-11-02T12:26:59.701Z}
{startDate: 2019-11-02T12:27:00.701Z, endDate: 2019-11-02T12:56:59.701Z}
{startDate: 2019-11-02T12:57:00.701Z, endDate: 2019-11-02T13:26:59.701Z}
{startDate: 2019-11-02T13:27:00.701Z, endDate: 2019-11-02T13:31:00.701Z}
]

最佳答案

使用 moment-range 插入

window['moment-range'].extendMoment(moment);

const start = moment('2019-11-02T11:57:00.701Z');
const end = moment('2019-11-02T13:31:00.701Z');
const range = moment.range(start, end);

const rangeBy = range.by('minutes', { step: 30 });

const res = Array.from(rangeBy).map(m => ({
'startTime': m.toISOString(),
'endTime': m.add(29, 'm').add(59, 's').toISOString()
}))

if(moment(res[res.length -1].endTime).isAfter(end)) {
res[res.length -1].endTime = end.toISOString();
}

console.log(res)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-range/4.0.1/moment-range.js"></script>

关于javascript - 如何将时刻日期从时隙拆分为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694737/

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