var HandleContinuityDate = function (beginTime, endTime) {
this.beginTime = beginTime;
this.endTime = endTime;
}
//根据时间字符串计算时间戳
HandleContinuityDate.calcTimestamp = function (datestr) {
var arr = datestr.split(" ");
var date = arr[0];//日期
var arr0 = date.split("-");
var year = 2017, month = 0, day = 1;
switch (arr0.length) {
case 0:
break;
case 1:
year = arr0[0];
break;
case 2:
year = arr0[0], month = arr0[1] - 1;
break;
case 3:
year = arr0[0], month = arr0[1] - 1, day = arr0[2];
break;
}
return Date.parse(new Date(year, month, day)) / 1000;//秒
}
//时间戳转时间字符串
HandleContinuityDate.getLocalTime = function (nS) {
var date = new Date(nS * 1000);//毫秒
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = month > 9 ? month : ("0" + month);
var day = date.getDate();
day = day > 9 ? day : ("0" + day);
return year + "-" + month + "-" + day;
}
//获得连续时间
HandleContinuityDate.prototype.getContinuityDate = function () {
var continuityDateArr = [];
var beginTime = HandleContinuityDate.calcTimestamp(this.beginTime);
var endTime = HandleContinuityDate.calcTimestamp(this.endTime);
var everyDayNs = 60 * 60 * 24;
var beginTimeEndTimeLength = (endTime - beginTime) / everyDayNs;
continuityDateArr.push(this.beginTime);
for (var i = 0; i < beginTimeEndTimeLength; i++) {
beginTime += everyDayNs;
if (beginTime >= endTime) {
beginTime = HandleContinuityDate.getLocalTime(endTime);
continuityDateArr.push(beginTime);
} else {
continuityDateArr.push(HandleContinuityDate.getLocalTime(beginTime));
}
}
return continuityDateArr;
}
var date = new HandleContinuityDate("2017-02-27", "2018-03-31");
var continuit = date.getContinuityDate();
console.log(continuit);