<script type="text/javascript"> /* * 思路: 把开始日期、结束日期分别存进两个数组,从开始时间的第二个元素去比较结束时间的第一个元素。 * 如此循环判断,如果小于或者等于,就代表时间段有交叉,直接跳出循环. */ startTime = ["2018/5/25", "2018/11/4", "2018/11/11", "2018/12/1", "2018/12/5", "2018/12/4"]; endTime = ["2018/5/30", "2018/11/5", "2018/11/14", "2018/12/4", "2018/12/14", "2019/2/28"]; /* *@params "2018/5/25" *@return "20180525" */ var getFormatDate = str => { let date = str.split('/'); let month = zeroFill(date[1]); let day = zeroFill(date[2]); return date[0]+month+day; }; var zeroFill = obj => { obj = obj + ''; if(obj.length == 1){ obj = '0'+obj; } return obj; }; let startArr = [],endArr = []; startTime.map((item)=>{ startArr.push(getFormatDate(item)); }); endTime.map((item)=>{ endArr.push(getFormatDate(item)); }); for(let i=1;i<startArr.length;i++){ if (startArr[i] <= endArr[i-1]){ alert("时间段存在重叠!"); break; } } </script>
转载来源:https://blog.csdn.net/weixin_41190571/article/details/88039717
标签:11,12,obj,重叠,交叉,js,let,2018,date From: https://www.cnblogs.com/hao-1234-1234/p/17072025.html