//四个时间段的拼接
joinDayTime(t1, t2, t3, t4) {
let n = '';
if(this.dateToString(t1)) n += this.dateToString(t1) + ','
if(this.dateToString(t2)) n += this.dateToString(t2) + ','
if(this.dateToString(t3)) n += this.dateToString(t3) + ','
if(this.dateToString(t4)) n += this.dateToString(t4) + ','
if(n.length) {
let m = this.joinTime(n.substr(0, n.length - 1))
m.length = 4
return m.join(',')
}else {
return null
}
},
//合并时间方法 思路 转换为数字区间,判断大小在转换为 时间
joinTime(value) {
//数间排序
var merge = function (intervals) {
if (intervals.length === 0) return []
let res = []
intervals.sort((a, b) => a[0] - b[0]) //数组首位排序函数sort
let candidate = intervals[0] //排序后选择第一个数组 (临时空间)
for (let i = 1; i < intervals.length; i++) {
let cur = intervals[i] //从第二个开始取cur
if (candidate[1] >= cur[0]) { // 有重合 能合并
candidate[1] = Math.max(cur[1], candidate[1]) // 左端不变 右端取大的
} else { // 不重合 不能合并
res.push(candidate)
candidate = cur //把cur放进去临时控件
}
}
res.push(candidate)
return res
};
//拼成统一的四位数
let formatZero = function(num) {
if(String(num).length > 4) return num;
return (Array(4).join(0) + num).slice(-4);
}
// 数转换为时间
var getTime = function (num) {
return formatZero(num).slice(0, 2) + ':' + formatZero(num).slice(2);
}
var reg=/:/g;
let time = value.toString().split(',')
for (var i = 0; i < time.length; i++) {
time[i] = time[i].replace(reg,'').split('-')
for (var j = 0; j < time[i].length; j++) {
time[i][j] = parseInt(time[i][j])
}
}
let newdate = merge(time)
for (let k = 0; k < newdate.length; k++) {
newdate[k] = getTime(newdate[k][0]) + "-" + getTime(newdate[k][1])
}
return newdate
},