var myDate = new Date(); var cyear = myDate.getFullYear(); var cmonth = myDate.getMonth(); var cday = myDate.getDate(); var hours = myDate.getHours(); //获取当前小时 var minutes = myDate.getMinutes(); //获取当前分钟 var seconds = myDate.getSeconds(); var startDate, endDate; var cut; var space = 5; var all = '[{'; cmonth = cmonth + 1; if (cmonth < 10) { cmonth = '0' + cmonth; } if (cday < 10) { cday = '0' + cday; } var created = cyear + '-' + cmonth + '-' + cday; var shift = 'Y'; if (shift == 'B') {//白班 startDate = created + ' 08:30'; endDate = created + ' 20:30'; } else if (shift == 'Y') {//夜班 cday = myDate.getDate() - 1; created1 = cyear + '-' + cmonth + '-' + cday; startDate = created1 + ' 20:30'; endDate = created + ' 08:30'; } var aa = new Date(startDate); var bb = new Date(endDate); var c = 5; space = space * 60 * 1000; var endTime = bb.getTime(); var startTime = aa.getTime(); var mod = endTime - startTime; var Start1 = '2023-06-26 22:00:00'; var End1 = '2023-06-26 22:10:00'; var Start2 = '2023-06-26 23:30:00'; var End2 = '2023-06-26 24:00:00'; var Start3 = '2023-06-27 03:00:00'; var End3 = '2023-06-27 03:10:00'; var Start4 = '2023-06-27 05:10:00'; var End4 = '2023-06-27 08:30:00'; console.log(mod); console.log(space); while (mod >= space) {//不足五分钟 var d = new Date(); d.setTime(startTime + space); var kun = 'date_now'; all = all + kun + ":" + "'" + d + "'" + ','; var formatDate1 = new Date(d); var formatDate2 = new Date(Start1); var formatDate3 = new Date(End1); var formatDate4 = new Date(Start2); var formatDate5 = new Date(End2); var formatDate6 = new Date(Start3); var formatDate7 = new Date(End3); var formatDate8 = new Date(Start4); var formatDate9 = new Date(End4); if (formatDate1 >= formatDate2 && formatDate1 <= formatDate3) {//大于Start1小于End1 d = Start1; } if (formatDate1 > formatDate3 && formatDate1 < formatDate4) { cut = d.getTime() - (formatDate3.getTime() - formatDate2.getTime());//大于End1小于Start2 d.setTime(cut); } if (formatDate1 >= formatDate4 && formatDate1 <= formatDate5) {//大于Start2小于End2 cut = formatDate4.getTime() - (formatDate3.getTime() - formatDate2.getTime())//getTime()不能直接作用于字符串对象 // d.setTime(cut); } if (formatDate1 > formatDate5 && formatDate1 < formatDate6) {//大于End2小于Start3 cut = d.getTime() - (formatDate3.getTime() - formatDate2.getTime()) - (formatDate5.getTime() - formatDate4.getTime()); d.setTime(cut); } if (formatDate1 >= formatDate6 && formatDate1 <= formatDate7) {//大于Start3小于End3 cut = formatDate6.getTime() - (formatDate3.getTime() - formatDate2.getTime()) - (formatDate5.getTime() - formatDate4.getTime()); d.setTime(cut); } if (formatDate1 > formatDate7 && formatDate1 < formatDate8) {//大于End3小于Start4 cut = d.getTime() - (formatDate3.getTime() - formatDate2.getTime()) - (formatDate5.getTime() - formatDate4.getTime()) - (formatDate7.getTime() - formatDate6.getTime()); d.setTime(cut); } if (formatDate1 >= formatDate8 && formatDate1 <= formatDate9) {//大于Start4小于End4 cut = formatDate8.getTime() - (formatDate3.getTime() - formatDate2.getTime()) - (formatDate5.getTime() - formatDate4.getTime()) - (formatDate7.getTime() - formatDate6.getTime()); d.setTime(cut); } var startDate1 = new Date(startDate); var stopDate = new Date(d); var startTime1 = startDate1.getTime(); var stopTime = stopDate.getTime(); var cTime = Number(stopTime) - Number(startTime1); var secondTime = cTime / 1000 / 60;//时间差 kun = 'secondTime'; all = all + kun + ":" + "'" + secondTime + "'" + ','; var O_SUM1 = 10; var number = 5; if (shift == 'Y') { number = O_SUM1 - number; kun = 'number'; all = all + kun + ":" + "'" + number + "'" ; } mod = mod - space; startTime = startTime + space; all = all + '},'; } all = all.substring(0, all.length - 1); all = all + ']'; var all1 = all console.log(all1); |