用vue获取多个时间段的交集
时间: 2023-05-17 18:05:26 浏览: 87
可以使用 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('没有交集');
}
```
这段代码可以获取多个时间段的交集,并输出交集的起始时间和结束时间。
阅读全文
相关推荐


















