Var now = new Date (); // current Date
Var nowDayOfWeek = (now. getDay () = 0 )? 7: now. getDay ()-1; // Today is the day of the week. Monday = 0, Sunday = 6
Var nowDay = now. getDate (); // current day
Var nowMonth = now. getMonth (); // current month value (January = months = 11)
Var nowMonReal = now. getMonth () + 1; // actual number of the current month
Var nowYear = now. getFullYear (); // current year
// Date + day
Function AddDays (d, n ){
Var t = new Date (d); // copy and operate the new object to avoid modifying the original object
T. setDate (t. getDate () + n );
Return t;
}
// Date + month. Day to date. If this date does not exist in the target month, it is set to the last day.
Function AddMonths (d, n ){
Var t = new Date (d );
T. setMonth (t. getMonth () + n );
If (t. getDate ()! = D. getDate () {t. setDate (0 );}
Return t;
}
// Date + year. If the date does not exist in the target month, the date is set to the last day.
Function AddYears (d, n ){
Var t = new Date (d );
T. setFullYear (t. getFullYear () + n );
If (t. getDate ()! = D. getDate () {t. setDate (0 );}
Return t;
}
// Obtain the start month of the current quarter
Function getQuarterStartMonth (){
If (nowMonth <= 2) {return 0 ;}
Else if (nowMonth <= 5) {return 3 ;}
Else if (nowMonth <= 8) {return 6 ;}
Else {return 9 ;}
}
// Monday
Function getWeekStartDate (){
Return AddDays (now,-nowDayOfWeek );
}
// Sunday. Monday + 6 days
Function getWeekEndDate (){
Return AddDays (getWeekStartDate (), 6 );
}
// The beginning of the month
Function getMonthStartDate (){
Return new Date (nowYear, nowMonth, 1 );
}
// The end of the month. Early next month-1 day
Function getMonthEndDate (){
Return AddDays (AddMonths (getMonthStartDate (), 1),-1 );
}
// At the beginning of the quarter
Function getQuarterStartDate (){
Return new Date (nowYear, getQuarterStartMonth (), 1 );
}
// End of quarter. Early next season-1 day
Function getQuarterEndDate (){
Return AddDays (AddMonths (getQuarterStartDate (), 3),-1 );
}