/ ** * Get This week, in the quarter, this month, last month, the start date, end date * / var now = new new a Date (); // current date var nowDayOfWeek = now.getDay (); // today this week the first few days var nowDay = now.getDate (); // current date var nowMonth = now.getMonth (); // current month var nowYear = now.getYear (); // current year nowYear + = (nowYear <2000 ?) 1900: 0; // var = new new lastMonthDate a date (); // last month, date lastMonthDate.setDate (1); lastMonthDate.setMonth (lastMonthDate.getMonth () - 1); var lastyear = lastMonthDate.getYear (); lastMonthDate.getMonth lastMonth = var (); // date format: the MM-dd-YYYY function the formatDate (dATE) { var myyear the Date.getFullYear = (); var MyMonth date.getMonth = () +. 1; var = dATE MyWeekDay .getDate (); IF (MyMonth < 10) { mymonth = "0" + mymonth; } if (myweekday < 10) { myweekday = "0" + myweekday; } return (myyear + "-" + mymonth + "-" + myweekday); } //获得某月的天数 function getMonthDays(myMonth) { var monthStartDate = new Date(nowYear, myMonth, 1); var monthEndDate = new Date(nowYear, myMonth + 1, 1); var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24); return days; } //获得本季度的开始月份 function getQuarterStartMonth() { var quarterStartMonth = 0; if (nowMonth < 3) { quarterStartMonth = 0; } IF (2 <nowMonth && nowMonth <. 6) { =. 3 quarterStartMonth; } IF (. 5 <nowMonth && nowMonth <. 9) { quarterStartMonth. 6 =; } IF (nowMonth>. 8) { quarterStartMonth =. 9; } return quarterStartMonth; } // get the week start date function getWeekStartDate () { a date new new weekStartDate = var (nowYear, nowMonth, nowDay - nowDayOfWeek); return the formatDate (weekStartDate); } // get the week ending date function getWeekEndDate () { var = new new weekEndDate a date (nowYear, nowMonth, nowDay + (. 6 - nowDayOfWeek)); return the formatDate (weekEndDate); } // get week start date function getLastWeekStartDate () { var = new new weekStartDate a Date (nowYear, nowMonth, nowDay - nowDayOfWeek -. 7); return the formatDate (weekStartDate); } // end get last date function getLastWeekEndDate () { var = new new weekEndDate a Date (nowYear, nowMonth, nowDay - nowDayOfWeek -. 1); return the formatDate (weekEndDate); } // get the start date in the month function getMonthStartDate () { var = new new monthStartDate a date (nowYear, nowMonth,. 1); return the formatDate (monthStartDate); } / / end date in the month obtained function getMonthEndDate () { var = new new monthEndDate a date (nowYear, nowMonth, getMonthDays (nowMonth)); return the formatDate (monthEndDate); } // start time obtained month getLastMonthStartDate function () { var = new new lastMonthStartDate a Date (nowYear, lastMonth,. 1); return the formatDate (lastMonthStartDate); } // end get month time function getLastMonthEndDate () { var = new new lastMonthEndDate a Date (nowYear, lastMonth, getMonthDays (lastMonth )); return the formatDate (lastMonthEndDate); } // get the quarter start date function getQuarterStartDate () { var = new new quarterStartDate a date (nowYear, getQuarterStartMonth (),. 1); return the formatDate (quarterStartDate); } // or present end date quarter function getQuarterEndDate () { var quarterEndMonth getQuarterStartMonth = () + 2; var = new new quarterStartDate a date (nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); return formatDate(quarterStartDate); }
The first day and calculate the monthly js / week last day
Because the project needs to pass this week encountered in the development of start and end times, as well as starting and ending time to the background last week, he had worked for a long time, and finally write out a set, write articles for the convenience of their own memories, but also the need to share people, limited, badly written, please forgive me:
getDateStr3 function to the time of the object into a string yy-mm-dd, to facilitate transfer value;
getWeekStartAndEnd function is to obtain the circumferential beginning and ending time, and with getDateStr3 converted into a string array, wherein the front 0 represents the current parameter weeks, -1 week on week -2 representatives, and so, in turn, may be a on behalf of the following week;
getMonthStartAndEnd function is to obtain monthly beginning and ending time, mass participation ibid.
//获取当前日期yy-mm-dd //date 为时间对象 function getDateStr3(date) { var year = ""; var month = ""; var day = ""; var now = date; year = ""+now.getFullYear(); if((now.getMonth()+1)<10){ month = "0"+(now.getMonth()+1); }else{ month = ""+(now.getMonth()+1); } if((now.getDate())<10){ day = "0"+(now.getDate()); } The else { Day = "" + (now.getDate ()); } return year + "-" + month The + "-" + Day; } / * * * obtained relative to the current peripheral AddWeekCount a periphery of the start and end dates * AddWeekCount is 0 for -1 for the current week is one week for the next one and so represents a peripheral * * * / function getWeekStartAndEnd (AddWeekCount) { // start and end dates array var StartStop = new new the array (); // day milliseconds var millisecond = 1000 * 60 * 60 * 24 ; // get the current time var currentDate = new new a Date (); //With respect to a circumferential AddWeekCount current date date currentDate = new new a Date (currentDate.getTime () + (millisecond. 7 * * AddWeekCount)); // return date is a day of the week var Week = currentDate.getDay (); // return date is one day a month in the var month the = currentDate.getDate (); // minus the number of days var minusDay = 0 = week week - 1: 6!? ; // get the first day of the current week var currentWeekFirstDay = new new a Date (currentDate.getTime () - (millisecond * minusDay)); // get the last day of the current week var currentWeekLastDay = new newDATE (currentWeekFirstDay.getTime () + (millisecond. 6 * )); // add to the array startStop.push (getDateStr3 (currentWeekFirstDay)); startStop.push (getDateStr3 (currentWeekLastDay)); return StartStop; } / * * * to obtain a relatively AddMonthCount month month start and end dates * AddMonthCount is 0 for the month is -1 for one month for the next month and so represents * ** * / function getMonthStartAndEnd (AddMonthCount) { // start and end dates array var StartStop = new new the array (); // get the current time var currentDate = new new a Date (); var month The currentDate.getMonth = () +AddMonthCount; IF (month The <0 ) { var n-= the parseInt ((- month The) / 12 is); month The n-+ * = 12 is ; currentDate.setFullYear (currentDate.getFullYear () - n-); } currentDate = new new a Date (currentDate. setMonth (month the)); // get the current month 0-11 var currentMonth = currentDate.getMonth (); // get the current year four-year var currentYear = currentDate.getFullYear (); // get the first day of the month var currentMonthFirstDay = new new a Date (currentYear, the currentMonth,. 1 ); //Obtained on the last day of the month var currentMonthLastDay = new new a Date (currentYear, the currentMonth. 1 +, 0 ); // add to the array startStop.push (getDateStr3 (currentMonthFirstDay)); startStop.push (getDateStr3 (currentMonthLastDay)); // Returns return StartStop; }