ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JS 多个时间段合并取并集,得到一个或多个时间段

2022-05-13 15:31:09  阅读:166  来源: 互联网

标签:index arr temp 多个 JS item length 时间段 2022


import dayjs from 'dayjs'; // 一个用来格式化时间的库,也可用其他代替
//根据时间开始结束日期,生成连续的时间数组(以天为单位)
  export function makeDateBetweenArr(startDate, endDate) {     let dates = [];     const theDate = new Date(startDate);     while (theDate < new Date(endDate)) {       dates = [...dates, dayjs(new Date(theDate)).format('YYYY-MM-DD')];       theDate.setDate(theDate.getDate() + 1);     }     dates = [...dates, dayjs(new Date(theDate)).format('YYYY-MM-DD')];     return dates;   }

// 快速排序 export function hurryShift(arr) { if (arr.length === 1 || arr.length === 0) { return arr; } let fix = arr[0]; let left = []; let right = []; for(let i = 1; i< arr.length; i++) { if(arr[i] <= fix) { left.push(arr[i]); } if(arr[i] > fix) { right.push(arr[i]); } } return hurryShift(left).concat([fix],hurryShift(right)); } // 算法:根据多个起止时间段日期,凡是有日期重叠的时间段都要合并,得出一个或多个时间段 export function calculateDates(arr) { // 根据起止日期,把时间段补全为每一天组成的时间段 const dates = arr.map(item => makeDateBetweenArr(item.startDate, item.endDate)); // 把所有时间段的每一天push到一个数组里 const allDates = []; dates.forEach(item => allDates.push(...item)); // 把数组去重,日期相同的天数去重 if(allDates.length === 0) { return []; } const onlyDates = Array.from(new Set(allDates)); // 每天转化为时间戳,进行快速排序,时间早的放前面 const times = onlyDates.map(item => new Date(item).getTime()); const orderTimes = hurryShift(times); // 算法核心:根据相邻日期的时间戳差值是否大于一天的毫秒数判断日期是否连续,不连续则为新的时间段的起止日期 // 把起止日期push到一个数组里,数组长度必为偶数 const temp = []; if(orderTimes.length === 1) { temp.push(...[orderTimes[0],orderTimes[0]]); }else { orderTimes.forEach((item, index) => { if(index === 0) { temp.push(dayjs(item).format('YYYY-MM-DD')); }else { if(item -orderTimes[index - 1] > 86400000) { temp.push(orderTimes[index - 1]); temp.push(dayjs(item).format('YYYY-MM-DD'));
} if(index === orderTimes.length - 1) { temp.push(dayjs(item).format('YYYY-MM-DD'));
} } }) } // 把结果数组根据每2个的长度截断为新的数组,第一个值为时间段的开始日期,第二个为结束日期 let result = []; if(temp.length > 0) { let index = 0; let resIndex = 0; result = new Array(Math.ceil(temp.length / 2)); while(index < temp.length) { result[resIndex++] = temp.slice(index ,(index+=2)) } } return result; };

// 示例
calculateDates([
{startDate: '2022-05-12', endDate: '2022-05-25'},
{startDate: '2022-05-15', endDate: '2022-05-30'},
{startDate: '2022-06-15', endDate: '2022-06-20'},
{startDate: '2022-06-10', endDate: '2022-06-25'},
]) 
// 结果:
  1. [
  2.   ['2022-05-12', '2022-05-30']
  3.   ['2022-06-10', '2022-06-25']
  4. ]
 

 

 

标签:index,arr,temp,多个,JS,item,length,时间段,2022
来源: https://www.cnblogs.com/justif/p/16266861.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有