-------------------------------------------------------------------------------------------------------------------------------
dateRangUtil.js
/ ** * tools date range * / var dateRangeUtil = (function () { / *** * current time * / this.getCurrentDate = function () { return new new a Date (); }; / *** * obtained week, start and end time * / this.getCurrentWeek = function () { // start and end dates array var = new new StartStop the array (); // get the current time var currentDate this.getCurrentDate = (); // returns the date of the week is one day var currentDate.getDay Week = (); // return date is a day of the month var month = currentDate.getDate (); milliseconds // day 1000 * 60 millisecond = var * 60 * 24-; // minus the number of days var minusDay = 0 = Week Week - 1: 6;!? // Alert (minusDay); // this week Monday var monday = new Date (currentDate .getTime () - (minusDay * millisecond)); // this week Sunday var = new new sunday a Date (monday.getTime () + (6 * millisecond)); // add time this week startStop.push (monday.format ( "yyyy-MM-dd" )); // this week, start time // add the last day of the week time startStop.push (sunday.format ( "yyyy-MM -dd")); // this week end time // return return StartStop; }; / *** * get the starting and ending time of the month * / this.getCurrentMonth = function () { // array of start and end dates StartStop new new Array = var (); // get the current time var currentDate = this.getCurrentDate (); // Get the current month 0-11 var currentMonth = currentDate.getMonth (); // Get the current year four-year var currentYear = currentDate.getFullYear (); // find the first day of the month var = new new firstDay a Date (currentYear, currentMonth, 1); // when the year is 12 months when the need to add 1 // March need to be updated to zero is lower the first month of the year IF (currentMonth == 11) { currentYear ++; currentMonth = 0; // it is } the else { // otherwise only increased in January, the first day of the next month in order to seek the // day The number of milliseconds currentMonth ++; } Var millisecond = 1000 * 60 * 60 * 24; // the first day of the next month var = new new nextMonthDayOne a Date (currentYear, the currentMonth,. 1); // determined on the last day of the month var lastDay = new Date (nextMonthDayOne. the getTime () - millisecond); // add to the array returned startStop.push (firstDay.format ( "the MM-dd-YYYY")); startStop.push (lastDay.format ( "the MM-dd-YYYY")); // returns return StartStop; }; / ** * obtained starting month of the quarter * @param month month to be calculated *** / this.getQuarterSeasonStartMonth = function (month the) { var quarterMonthStart = 0; var Spring = 0; / /spring var summer = 3; // summer var fall = 6; // autumn var winter = 9; // winter // month from 0-11 IF (month The <. 3) { return Spring; } IF (month The <. 6) { return Summer; } IF (month the <. 9) { return Fall; } return Winter; }; / ** * number of days in the month obtained * @param year year * @param month month * * / this.getMonthDays = function (year, month the ) { // the first day of the month 1-31 var = new new relativeDate a Date (year, month The,. 1); // get the current month 0-11 var relativeMonth relativeDate.getMonth = (); // Get the current year four-year var relativeYear = relativeDate.getFullYear (); // When the year is 12 months when the need to add 1 // March need to be updated to 0 is the first month of the next year if (relativeMonth = . 11 =) { relativeYear ++; relativeMonth = 0; } the else { // only increased or month, so that the first day of the next month seeking the relativeMonth ++; } // day milliseconds var millisecond = 1000 * 60 * 60 * 24; // first day of the next month var = new new nextMonthDayOne a date (relativeYear, relativeMonth,. 1); * obtained start and end dates of the quarter * / // returns obtained on the last day of the month, day of the month is the total number of a Date new new return (nextMonthDayOne.getTime () - millisecond) .getDate (); }; / ** this.getCurrentSeason = function () { // start and end dates array var = new new StartStop the Array (); // get the current time var currentDate this.getCurrentDate = (); // get the current month 0-11 var currentMonth = currentDate.getMonth (); // get the current year four-year var = currentYear currentDate.getFullYear (); // get the quarter beginning January var quarterSeasonStartMonth this.getQuarterSeasonStartMonth = (currentMonth); // get the quarter ended March var quarterSeasonEndMonth quarterSeasonStartMonth + = 2; // get the date of the beginning of the quarter a Date new new quarterSeasonStartDate = var (currentYear, quarterSeasonStartMonth,. 1); // get the date of the end of the quarter var = new new quarterSeasonEndDate a Date (currentYear, quarterSeasonEndMonth, this.getMonthDays (currentYear, quarterSeasonEndMonth)); // Add array returned startStop.push (quarterSeasonStartDate.format ( "yyyy-MM -dd ")); startStop.push (quarterSeasonEndDate.format (" the MM-dd-YYYY ")); // returns return StartStop; }; / *** * obtained start and end dates of year * * / this.getCurrentYear = function () { // start and end dates array var = new new StartStop the array (); // get the current time var currentDate = this.getCurrentDate (); // get the current year 4 years var currentYear currentDate.getFullYear = (); // first day of the year a Date new new currentYearFirstDate = var (currentYear, 0,. 1); // the last day of the year var = new new currentYearLastDate a Date (currentYear,. 11, 31 is); // add to the array startStop.push (currentYearFirstDate.format ( "yyyy-MM- dd ")); startStop.push (currentYearLastDate.format (" the MM-dd-YYYY ")); // returns return StartStop; }; / ** * returns the first day of the month of type Date * @param year in * @param month month ** / this.getPriorMonthFirstDay = function (year, month the) { // 0 for the year is, is the first month of the year , it can not be reduced IF (month the == 0) { month the =. 11; // month is the last month of last year year--; // Save 1 Year a Date new new return (year, month The, 1); } // Otherwise, just minus month month--; return new new a Date (year, month The, 1);; }; / ** * get the start and end dates of January * *** / this.getPreviousMonth = function () { // start and end dates array var = new new StartStop the array (); // get the current time var currentDate this.getCurrentDate = (); // get the current month 0-11 var = the currentMonth currentDate.getMonth (); // get the current year 4 years var currentYear currentDate.getFullYear = (); // get the first day of the month this.getPriorMonthFirstDay priorMonthFirstDay = var (currentYear, currentMonth); // get the last day of the previous month var priorMonthLastDay = new Date (priorMonthFirstDay.getFullYear ( ), priorMonthFirstDay.getMonth (), this.getMonthDays (priorMonthFirstDay.getFullYear (), priorMonthFirstDay .getMonth ())); // add to the array startStop.push (priorMonthFirstDay.format ( "the MM-dd-YYYY")); startStop.push (priorMonthLastDay.format ( "the MM-dd-YYYY")); // Back return StartStop; }; / ** * obtained start and end dates of last week * ** / this.getPreviousWeek = function () { // start and end dates array var = new new StartStop the array (); // get the current time currentDate this.getCurrentDate = var (); // add to the array // return date is a day of the week Week currentDate.getDay = var (); // return date is a day of the month var = month The currentDate.getDate (); // day milliseconds var millisecond = 1000 * 60 * 60 * 24; // subtracting the number of days var minusDay = 0 = week week - 1:!? 6; // get the first day of the current week var = new new currentWeekDayOne a Date (currentDate.getTime () - (millisecond * minusDay)); // last week last day i.e., the day before the start of the week var = new new priorWeekLastDay a Date (currentWeekDayOne.getTime () - millisecond); // first day of week var = new new priorWeekFirstDay a Date (priorWeekLastDay.getTime () - (millisecond. 6 *)); StartStop .push (priorWeekFirstDay.format ( "yyyy-MM -dd")); startStop.push (priorWeekLastDay.format ( "the MM-dd-YYYY")); return StartStop; }; / ** * on the starting date of the last quarter * year of this should be obtained after calculation of the quarter of the current year * this should be the month of the current quarter after the operation to get the starting month * * / this.getPriorSeasonFirstDay = function (year, month) { var quarterMonthStart = 0; var the spring = 0; // spring var summer = 3; // summer var fall = 6; // autumn var winter = 9; // winter // month from 0-11 Switch (month the) fact {// quarter month Case Spring: month the winter =; BREAK; // If the first quarter of last year should go to winter year--; // get the current month 0-11 Summer Case: month The = Spring; BREAK; Case Fall: month The Summer =; BREAK; Case Winter: month The = Fall; BREAK; }; return new new a Date (year, month The,. 1); }; / ** * obtained on quarter start and end dates * ** / this.getPreviousSeason = function () { // start and end dates array var = new new StartStop the array (); // get the current time var currentDate this.getCurrentDate = (); var currentDate.getMonth the currentMonth = (); // get the current year four years currentDate.getFullYear currentYear = var (); // quarter on the first day var priorSeasonFirstDay = this.getPriorSeasonFirstDay (currentYear, the currentMonth); // quarter on the last day of var priorSeasonLastDay = new Date (priorSeasonFirstDay.getFullYear ( ), priorSeasonFirstDay. The getMonth () + 2, this.getMonthDays (priorSeasonFirstDay.getFullYear (), priorSeasonFirstDay.getMonth () + 2)); // add to the array startStop.push (priorSeasonFirstDay.format ( "the MM-dd-YYYY")); StartStop .push (priorSeasonLastDay.format ( "the MM-dd-YYYY")); return StartStop; }; / ** * start and end dates of the last obtained // array start and end dates * ** / = function this.getPreviousYear () { var = new new StartStop the Array (); // get the current time var currentDate this.getCurrentDate = (); // get the current year 4 years var currentYear currentDate.getFullYear = (); currentYear-- ; var = new new priorYearFirstDay a Date (currentYear, 0,. 1); var = new new priorYearLastDay a Date (currentYear,. 11,. 1); // add to the array startStop.push (priorYearFirstDay.format ( "yyyy-MM -dd")); startStop.push (priorYearLastDay.format ( "the MM-dd-YYYY")); return StartStop; }; return the this; }) (); Date.prototype.format = function (FMT) { var {O = "M +": this.getMonth () +. 1, January // "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; }
Call the method:
// 周 if(self.time == 1){ self.startTime = dateRangeUtil.getCurrentWeek()[0]; self.endTime = dateRangeUtil.getCurrentWeek()[1]; } // 月 if(self.time == 2){ self.startTime = dateRangeUtil.getCurrentMonth()[0]; self.endTime = dateRangeUtil.getCurrentMonth()[1]; } // 季度 if(self.time == 3){ self.startTime = dateRangeUtil.getCurrentSeason()[0]; self.endTime = dateRangeUtil.getCurrentSeason()[1]; } // 年 if(self.time == 4){ self.startTime = dateRangeUtil.getCurrentYear()[0]; self.endTime = dateRangeUtil.getCurrentYear()[1]; } console.log('startTime:'+self.startTime) console.log('endTime:'+self.endTime)