let input = [
"2018-4-6 3:00",
"2018-4-6 4:00",
"2018-4-6 5:00",
"2018-4-6 6:00",
"2018-4-6 7:00",
"2018-4-6 10:00",
"2018-4-6 11:00",
"2018-4-7 3:00",
"2018-4-7 4:00",
"2018-4-7 5:00",
"2018-4-7 6:00",
"2018-4-7 7:00",
"2018-4-8 3:00",
"2018-4-8 4:00",
"2018-4-8 5:00",
"2018-4-8 6:00",
"2018-4-8 7:00",
"2018-4-9 23:00",
"2018-4-10 00:00",
"2018-4-12 6:00"
];
//新建一个类
function HandleTime(){};
//定义类的原型方法
HandleTime.prototype.getTimeDuration = function(input) {
let ret = [];
//你的实现
for(i=0;i<input.length;i++){
let tempRet = [];
tempRet.push(input[i]);
for (j=i+1;j<input.length;j++){
if(!this.isContinue(input[j-1],input[j])) {
tempRet.push(this.endTime(input[j-1]));
i=j-1;
break;
}
}
ret.push(tempRet);
}
ret[ret.length-1].push(this.endTime(input[input.length-1]));
return ret;
}
HandleTime.prototype.getYear = function(str) {
let index = str.indexOf("-");
return str.slice(0,index);
}
HandleTime.prototype.getMonth = function(str) {
let index1 = str.indexOf("-");
let index2 = str.slice(index1+1).indexOf("-");
return str.slice(index1+1,index2);
}
HandleTime.prototype.getDay = function(str) {
let index = str.indexOf(" ");
if(str.charAt(index-2) == "-"){
return str.charAt(index-1);
}else{
return str.substr(index-2,2);
}
}
HandleTime.prototype.getTime = function(str) {
let index1 = str.indexOf(" ");
let index2 = str.indexOf(":");
return str.slice(index1+1,index2);
}
HandleTime.prototype.endTime = function(str){
let index1 = str.indexOf(" ");
let index2 = str.indexOf(":");
let time = str.slice(index1+1,index2);
if(time == "23"){
return str.slice(0,index1+1).concat("00",str.slice(index2));
}else{
return str.slice(0,index1+1).concat(parseInt(time)+1,str.slice(index2));
}
}
HandleTime.prototype.isContinue = function(str1,str2){
if(this.getYear(str1) != this.getYear(str2)){
return false;
}
if(this.getMonth(str1) != this.getMonth(str2)) {
return false;
}
if(this.getDay(str1) != this.getDay(str2)) {
if(parseInt(this.getDay(str1))+1 == this.getDay(str2) &&
this.getTime(str1) == "23" && this.getTime(str2) == "00") {
return true;
}
return false;
}
if(parseInt(this.getTime(str1))+1 != this.getTime(str2) ) {
return false;
}
return true;
}
var handleTime = new HandleTime();
handleTime.input = input;
console.log(handleTime.input);
let out = handleTime.getTimeDuration(input);
console.log(out);
console