Js date calculation and quick acquisition of weeks, months, and quarter start and end days

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.