Js Code
Copy codeThe Code is as follows:
/**
* Obtain the start date and end date of this week, this quarter, this month, and last month.
*/
Var now = new Date (); // current Date
Var nowDayOfWeek = now. getDay (); // the day of the week today
Var nowDay = now. getDate (); // current day
Var nowMonth = now. getMonth (); // current month
Var nowYear = now. getYear (); // current year
NowYear + = (nowYear <2000 )? 1900: 0 ;//
Var lastMonthDate = new Date (); // Date of the previous month
LastMonthDate. setDate (1 );
LastMonthDate. setMonth (lastMonthDate. getMonth ()-1 );
Var lastYear = lastMonthDate. getYear ();
Var lastMonth = lastMonthDate. getMonth ();
// Pattern Date: yyyy-MM-dd
Function formatDate (date ){
Var myyear = date. getFullYear ();
Var mymonth = date. getMonth () + 1;
Var myweekday = date. getDate ();
If (mymonth <10 ){
Mymonth = "0" + mymonth;
}
If (myweekday <10 ){
Myweekday = "0" + myweekday;
}
Return (myyear + "-" + mymonth + "-" + myweekday );
}
// Obtain the number of days in a month
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;
}
// Obtain the start month of the current quarter
Function getQuarterStartMonth (){
Var quarterStartMonth = 0;
If (nowMonth <3 ){
QuarterStartMonth = 0;
}
If (2 <nowMonth & nowMonth <6 ){
QuarterStartMonth = 3;
}
If (5 <nowMonth & nowMonth <9 ){
QuarterStartMonth = 6;
}
If (nowMonth> 8 ){
QuarterStartMonth = 9;
}
Return quarterStartMonth;
}
// Obtain the start date of the week
Function getWeekStartDate (){
Var weekStartDate = new Date (nowYear, nowMonth, nowDay-nowDayOfWeek );
Return formatDate (weekStartDate );
}
// Obtain the end date of the week
Function getWeekEndDate (){
Var weekEndDate = new Date (nowYear, nowMonth, nowDay + (6-nowDayOfWeek ));
Return formatDate (weekEndDate );
}
// Obtain the start date of this month
Function getMonthStartDate (){
Var monthStartDate = new Date (nowYear, nowMonth, 1 );
Return formatDate (monthStartDate );
}
// Obtain the end date of this month
Function getMonthEndDate (){
Var monthEndDate = new Date (nowYear, nowMonth, getMonthDays (nowMonth ));
Return formatDate (monthEndDate );
}
// Obtain the start time of the previous month
Function getLastMonthStartDate (){
Var lastMonthStartDate = new Date (nowYear, lastMonth, 1 );
Return formatDate (lastMonthStartDate );
}
// Get the last month stop time
Function getLastMonthEndDate (){
Var lastMonthEndDate = new Date (nowYear, lastMonth, getMonthDays (lastMonth ));
Return formatDate (lastMonthEndDate );
}
// Obtain the start date of the current quarter
Function getQuarterStartDate (){
Var quarterStartDate = new Date (nowYear, getQuarterStartMonth (), 1 );
Return formatDate (quarterStartDate );
}
// Or the end date of the current quarter
Function getQuarterEndDate (){
Var quarterEndMonth = getQuarterStartMonth () + 2;
Var quarterStartDate = new Date (nowYear, quarterEndMonth, getMonthDays (quarterEndMonth ));
Return formatDate (quarterStartDate );
}