/**
* 返回指定长度的天数集合
*
* @param {time} 时间
* @param {len} 长度
* @param {direction} 方向: 1: 前几天; 2: 后几天; 3:前后几天 默认 3
* @return {Array} 数组
*
* @example date.getDays('2018-1-29', 6) // -> ["2018-1-26", "2018-1-27", "2018-1-28", "2018-1-29", "2018-1-30", "2018-1-31", "2018-2-1"]
*/
getDays(time, len, diretion) {
var tt = new Date(time);
var getDay = function(day) {
var t = new Date(time);
t.setDate(t.getDate() + day);
var m = t.getMonth()+1;
return t.getFullYear()+'-'+m+'-'+t.getDate();
}
var arr = [];
if (diretion === 1) {
for (var i = 1; i <= len; i++) {
arr.unshift(getDay(-i));
}
}else if(diretion === 2) {
for (var i = 1; i <= len; i++) {
arr.push(getDay(i));
}
}else {
for (var i = 1; i <= len; i++) {
arr.unshift(getDay(-i));
}
arr.push(tt.getFullYear()+'-'+(tt.getMonth()+1)+'-'+tt.getDate());
for (var i = 1; i <= len; i++) {
arr.push(getDay(i));
}
}
return diretion === 1 ? arr.concat([tt.getFullYear()+'-'+(tt.getMonth()+1)+'-'+tt.getDate()]) :
diretion === 2 ? [tt.getFullYear()+'-'+(tt.getMonth()+1)+'-'+tt.getDate()].concat(arr) : arr
}
/*获取某月有多少天*/
getMonthOfDay (time) {
var date = new Date(time)
var year = date.getFullYear()
var mouth = date.getMonth() + 1
var days
//当月份为二月时,根据闰年还是非闰年判断天数
if (mouth == 2) {
days = year % 4 == 0 ? 29 : 28
} else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
//月份为:1,3,5,7,8,10,12 时,为大月.则天数为31;
days = 31
} else {
//其他月份,天数为:30.
days = 30
}
return days
}
/*获取某年有多少天*/
getYearOfDay (time) {
var firstDayYear = this.getFirstDayOfYear(time);
var lastDayYear = this.getLastDayOfYear(time);
var numSecond = (new Date(lastDayYear).getTime() - new Date(firstDayYear).getTime())/1000;
return Math.ceil(numSecond/(24*3600));
}
/*获取某年的第一天*/
getFirstDayOfYear (time) {
var year = new Date(time).getFullYear();
return year + "-01-01 00:00:00";
}
/*获取某年最后一天*/
getLastDayOfYear (time) {
var year = new Date(time).getFullYear();
var dateString = year + "-12-01 00:00:00";
var endDay = this.getMonthOfDay(dateString);
return year + "-12-" + endDay + " 23:59:59";
}
/*获取某个日期是当年中的第几天*/
getDayOfYear (time) {
var firstDayYear = this.getFirstDayOfYear(time);
var numSecond = (new Date(time).getTime() - new Date(firstDayYear).getTime())/1000;
return Math.ceil(numSecond/(24*3600));
}
/*获取某个日期在这一年的第几周*/
getDayOfYearWeek (time) {
var numdays = this.getDayOfYear(time);
return Math.ceil(numdays / 7);
}