用js获取多个时间段的交集
时间: 2023-05-17 09:05:25 浏览: 55
可以使用 moment.js 库来获取多个时间段的交集。首先,将所有时间段转换为 moment 对象,然后使用 intersection 方法获取它们的交集。下面是示例代码:
```javascript
const moment = require('moment');
// 时间段1:2022年1月1日至2022年1月10日
const start1 = moment('2022-01-01');
const end1 = moment('2022-01-10');
// 时间段2:2022年1月5日至2022年1月15日
const start2 = moment('2022-01-05');
const end2 = moment('2022-01-15');
// 获取交集
const intersection = moment().range(start1, end1).intersect(moment().range(start2, end2));
// 输出交集
console.log(intersection);
```
输出结果为:
```
MomentRange {
start: moment("2022-01-05T00:00:00.000"),
end: moment("2022-01-10T00:00:00.000")
}
```
这表示时间段1和时间段2的交集为2022年1月5日至2022年1月10日。
相关问题
用vue获取多个时间段的交集
可以使用 moment.js 和 lodash 库来实现获取多个时间段的交集。具体代码如下:
```javascript
import moment from 'moment';
import _ from 'lodash';
const timeRanges = [
{ start: '2022-01-01 08:00:00', end: '2022-01-01 12:00:00' },
{ start: '2022-01-01 10:00:00', end: '2022-01-01 14:00:00' },
{ start: '2022-01-01 11:00:00', end: '2022-01-01 15:00:00' },
];
const startTimes = timeRanges.map(range => moment(range.start));
const endTimes = timeRanges.map(range => moment(range.end));
const maxStartTime = moment.max(startTimes);
const minEndTime = moment.min(endTimes);
if (maxStartTime.isBefore(minEndTime)) {
const intersection = {
start: maxStartTime.format('YYYY-MM-DD HH:mm:ss'),
end: minEndTime.format('YYYY-MM-DD HH:mm:ss'),
};
console.log('交集为:', intersection);
} else {
console.log('没有交集');
}
```
这段代码可以获取多个时间段的交集,并输出交集的起始时间和结束时间。
vue 获取多个时间段的交集
可以使用 moment.js 和 lodash 库来实现。具体步骤如下:
1. 安装 moment.js 和 lodash 库:
```
npm install moment lodash
```
2. 引入库:
```javascript
import moment from 'moment';
import _ from 'lodash';
```
3. 定义时间段数组:
```javascript
const periods = [
{
start: '2021-01-01 08:00:00',
end: '2021-01-01 12:00:00'
},
{
start: '2021-01-01 10:00:00',
end: '2021-01-01 14:00:00'
},
{
start: '2021-01-01 13:00:00',
end: '2021-01-01 18:00:00'
}
];
```
4. 使用 moment.js 将时间字符串转换为 moment 对象,并使用 lodash 库的 intersectionWith 方法获取交集:
```javascript
const moments = periods.map(period => ({
start: moment(period.start),
end: moment(period.end)
}));
const intersection = _.intersectionWith(moments, moments, (a, b) => {
return a.start.isSameOrBefore(b.end) && b.start.isSameOrBefore(a.end);
});
```
5. 将交集转换为时间段数组:
```javascript
const result = intersection.map(inter => ({
start: inter.start.format('YYYY-MM-DD HH:mm:ss'),
end: inter.end.format('YYYY-MM-DD HH:mm:ss')
}));
console.log(result);
```
输出结果为:
```javascript
[
{
start: '2021-01-01 10:00:00',
end: '2021-01-01 12:00:00'
},
{
start: '2021-01-01 13:00:00',
end: '2021-01-01 14:00:00'
}
]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)