var dateRangeUtil = (function () {
this.getCurrentDate = function () {
return new Date();
};
this.getCurrentWeek = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var week = currentDate.getDay();
var month = currentDate.getDate();
var millisecond = 1000 * 60 * 60 * 24;
var minusDay = week != 0 ? week - 1 : 6;
var monday = new Date(currentDate.getTime() - (minusDay * millisecond));
var sunday = new Date(monday.getTime() + (6 * millisecond));
startStop.push(new Date(monday).format("yyyy-MM-dd"));
startStop.push(new Date(sunday).format("yyyy-MM-dd"));
return startStop;
};
this.getLastMonthFristDay = function () {
var firstDate = new Date(new Date().getFullYear(), new Date().getMonth()-1, 1).format("yyyy-MM-dd");
return firstDate;
};
this.getLaseMonthWeek = function(date) {
var arr = date.split('-');
var year = arr[0];
var month = arr[1];
var day = arr[2];
var reg = date.match(/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/);
var pre_year = year;
var pre_month = parseInt(month) - 1;
if (pre_month === 0) {
pre_year = parseInt(pre_year) - 1;
pre_month = 12;
}
var pre_day = parseInt(day);
var pre_month_alldays = new Date(pre_year, pre_month, 0).getDate();
if (pre_day > pre_month_alldays) {
pre_day = pre_month_alldays;
}
if (pre_month < 10) {
pre_month = '0' + pre_month;
}
if (pre_day < 10) {
pre_day = '0' + pre_day;
}
var pre_month_day = pre_year + '-' + pre_month + '-' + pre_day;
return pre_month_day;
},
this.getCurrentMonth = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getFullYear();
var firstDay = new Date(currentYear, currentMonth, 1);
if (currentMonth == 11) {
currentYear++;
currentMonth = 0;
} else {
currentMonth++;
}
var millisecond = 1000 * 60 * 60 * 24;
var nextMonthDayOne = new Date(currentYear, currentMonth, 1);
var lastDay = new Date(nextMonthDayOne.getTime() - millisecond);
startStop.push(new Date(firstDay).format("yyyy-MM-dd"))
startStop.push(new Date(lastDay).format("yyyy-MM-dd"));
return startStop;
};
this.getQuarterSeasonStartMonth = function (month) {
var quarterMonthStart = 0;
var spring = 0;
var summer = 3;
var fall = 6;
var winter = 9;
if (month < 3) {
return spring;
}
if (month < 6) {
return summer;
}
if (month < 9) {
return fall;
}
return winter;
};
this.getMonthDays = function (year, month) {
var relativeDate = new Date(year, month, 1);
var relativeMonth = relativeDate.getMonth();
var relativeYear = relativeDate.getFullYear();
if (relativeMonth == 11) {
relativeYear++;
relativeMonth = 0;
} else {
relativeMonth++;
}
var millisecond = 1000 * 60 * 60 * 24;
var nextMonthDayOne = new Date(relativeYear, relativeMonth, 1);
return new Date(nextMonthDayOne.getTime() - millisecond).getDate();
};
this.getCurrentSeason = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getFullYear();
var quarterSeasonStartMonth = this.getQuarterSeasonStartMonth(currentMonth);
var quarterSeasonEndMonth = quarterSeasonStartMonth + 2;
var quarterSeasonStartDate = new Date(currentYear, quarterSeasonStartMonth, 1);
var quarterSeasonEndDate = new Date(currentYear, quarterSeasonEndMonth, this.getMonthDays(currentYear, quarterSeasonEndMonth));
startStop.push(quarterSeasonStartDate);
startStop.push(quarterSeasonEndDate);
return startStop;
};
this.getCurrentYear = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var currentYear = currentDate.getFullYear();
var currentYearFirstDate = new Date(currentYear, 0, 1);
var currentYearLastDate = new Date(currentYear, 11, 31);
startStop.push(currentYearFirstDate);
startStop.push(currentYearLastDate);
return startStop;
};
this.getPriorMonthFirstDay = function (year, month) {
if (month == 0) {
month = 11;
year--;
return new Date(year, month, 1);
}
month--;
return new Date(year, month, 1); ;
};
this.getPreviousMonth = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getFullYear();
var priorMonthFirstDay = this.getPriorMonthFirstDay(currentYear, currentMonth);
var priorMonthLastDay = new Date(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth(), this.getMonthDays(priorMonthFirstDay.getFullYear(), priorMonthFirstDay.getMonth()));
startStop.push(priorMonthFirstDay);
startStop.push(priorMonthLastDay);
return startStop;
};
this.getPreviousWeek = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var week = currentDate.getDay();
var month = currentDate.getDate();
var millisecond = 1000 * 60 * 60 * 24;
var minusDay = week != 0 ? week - 1 : 6;
var currentWeekDayOne = new Date(currentDate.getTime() - (millisecond * minusDay));
var priorWeekLastDay = new Date(currentWeekDayOne.getTime() - millisecond);
var priorWeekFirstDay = new Date(priorWeekLastDay.getTime() - (millisecond * 6));
startStop.push(priorWeekFirstDay);
startStop.push(priorWeekLastDay);
return startStop;
};
this.getPriorSeasonFirstDay = function (year, month) {
var quarterMonthStart = 0;
var spring = 0;
var summer = 3;
var fall = 6;
var winter = 9;
switch (month) {
case spring:
year--;
month = winter;
break;
case summer:
month = spring;
break;
case fall:
month = summer;
break;
case winter:
month = fall;
break;
};
return new Date(year, month, 1);
};
this.getPreviousSeason = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var currentMonth = currentDate.getMonth();
var currentYear = currentDate.getFullYear();
var priorSeasonFirstDay = this.getPriorSeasonFirstDay(currentYear, currentMonth);
var priorSeasonLastDay = new Date(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2, this.getMonthDays(priorSeasonFirstDay.getFullYear(), priorSeasonFirstDay.getMonth() + 2));
startStop.push(priorSeasonFirstDay);
startStop.push(priorSeasonLastDay);
return startStop;
};
this.getPreviousYear = function () {
var startStop = new Array();
var currentDate = this.getCurrentDate();
var currentYear = currentDate.getFullYear();
currentYear--;
var priorYearFirstDay = new Date(currentYear, 0, 1);
var priorYearLastDay = new Date(currentYear, 11, 1);
startStop.push(priorYearFirstDay);
startStop.push(priorYearLastDay);
return startStop;
};
this.getMonStart = function() {
var dt = new Date();
dt.setMonth(dt.getMonth() - 2);
dt.setDate(1);
var month = parseInt(dt.getMonth() + 1);
var day = dt.getDate();
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
return dt.getFullYear() + '-' + month + '-' + day;
},
this.getMonEnd = function() {
var date = new Date();
date.setMonth(date.getMonth() - 2);
var currentMonth = date.getMonth();
var nextMonth = ++currentMonth;
var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
var oneDay = 1000 * 60 * 60 * 24;
var lastTime = new Date(nextMonthFirstDay - oneDay);
var month = parseInt(lastTime.getMonth() + 1);
var day = lastTime.getDate();
if (month < 10) {
month = '0' + month
}
if (day < 10) {
day = '0' + day
}
return date.getFullYear() + '-' + month + '-' + day;
},
this.dateFormat = function(fmt, date) {
let ret;
if(typeof date ==='string'){
return date
}
const opt = {
"Y+": date.getFullYear().toString(),
"m+": (date.getMonth() + 1).toString(),
"d+": date.getDate().toString(),
"H+": date.getHours().toString(),
"M+": date.getMinutes().toString(),
"S+": date.getSeconds().toString()
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
};
};
return fmt;
};
this.formatDate = function(date) {
date = new Date(Date.parse(date.replace(/-/g, "/")));
var y = date.getFullYear();
console.log(y);
var m = date.getMonth() + 1;
m = m < 10 ? '0' + m : m;
var d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '-' + m + '-' + d;
}
this.getMyDay =function(date){
var week;
if(date.getDay()==0) week="周日";
if(date.getDay()==1) week="周一";
if(date.getDay()==2) week="周二";
if(date.getDay()==3) week="周三";
if(date.getDay()==4) week="周四";
if(date.getDay()==5) week="周五";
if(date.getDay()==6) week="周六";
return week;
}
Date.prototype.format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return fmt;
}
return this;
})();