JS gets a week, month, next month, a quarter of the start date, the end date and the judgment date of the week

Source: Internet
Author: User

  /** * Get start date, end date and judgment date of week, month, month, quarter*/    //formatted Date: YYYY-MM-DDfunctionformatdate (date) {varMyyear =date.getfullyear (); varMymonth = Date.getmonth () +1; varMyweekday =date.getdate (); if(Mymonth < 10) {Mymonth= "0" +Mymonth; }if(Myweekday < 10) {Myweekday= "0" +Myweekday; }return(myyear+ "-" +mymonth + "-" +myweekday); }//get the number of days in a monthfunctionGetmonthdays (parayear,paramonth) {varMonthstartdate =NewDate (Parayear, Paramonth, 1); varMonthenddate =NewDate (parayear, Paramonth + 1, 1); varDays = (monthenddate-monthstartdate)/(1000 * 60 * 60 * 24);returnDays ; }//get the start date for a weekfunctiongetweekstartdate (parayear,paramonth,paraday,paradayofweek) {varWeekstartdate =NewDate (Parayear, Paramonth, Paraday + 1-Paradayofweek); returnformatdate (weekstartdate); }//get the end date of a weekfunctiongetweekenddate (parayear,paramonth,paraday,paradayofweek) {varWeekenddate =NewDate (Parayear, Paramonth, Paraday + (7-Paradayofweek)); returnformatdate (weekenddate); }//get the start date of a monthfunctiongetmonthstartdate (parayear,paramonth) {varMonthstartdate =NewDate (Parayear, Paramonth, 1); returnformatdate (monthstartdate); }//get the end date of a monthfunctiongetmonthenddate (parayear,paramonth) {varMonthenddate =NewDate (Parayear,paramonth, Getmonthdays (Parayear,paramonth)); returnformatdate (monthenddate); }//get start time last monthfunctiongetlastmonthstartdate (parayear, lastmonth) {varLastmonthstartdate =NewDate (Parayear, Lastmonth, 1); returnformatdate (lastmonthstartdate); }//get last month end timefunctiongetlastmonthenddate (Parayear, Lastmonth, lastmonth) {varLastmonthenddate =NewDate (Parayear, Lastmonth, Getmonthdays (Lastmonth)); returnformatdate (lastmonthenddate); }//get the start date for a quarterfunctiongetquarterstartdate (Parayear,paraseason) {Switch(Paraseason) { Case' 1 ':returnparayear+ "-01-01";  Case' 2 ':returnparayear+ "-04-01";  Case' 3 ':returnparayear+ "-07-01";  Case' 4 ':returnparayear+ "-10-01"; }}//Get the end date for a quarterfunctiongetquarterenddate (Parayear,paraseason) {Switch(Paraseason) { Case' 1 ':returnparayear+ "-03-31";  Case' 2 ':returnparayear+ "-06-30";  Case' 3 ':returnparayear+ "-09-30";  Case' 4 ':returnparayear+ "-12-31"; }}//get the start date of a week of a yearfunctionGetbegindateofweek (parayear, weekindex) {varFirstDay =Getfirstweekbegday (parayear); //7*24*3600000 is the time of the week in milliseconds, (the date in JS is accurate to milliseconds)    varTime= (weekIndex-1) *7*24*3600000; varBeginday =FirstDay; //Reset the Date object date to timeBeginday.settime (firstday.valueof () +Time ); returnformatdate (beginday);} //get the end date of a week of a yearfunctionGetenddateofweek (parayear, weekindex) {varFirstDay =Getfirstweekbegday (parayear); //7*24*3600000 is the time of the week in milliseconds, (the date in JS is accurate to milliseconds)    varTime= (weekIndex-1) *7*24*3600000; varWeektime = 6*24*3600000; varEndday =FirstDay; //Reset the Date object date to timeEndday.settime (firstday.valueof () +weektime+Time ); returnformatdate (endday);} //get date is the week ordinal of a yearfunctionGetweekindex (dateobj) {varFirstDay =Getfirstweekbegday (Dateobj.getfullyear ()); if(Dateobj <firstday) {FirstDay= Getfirstweekbegday (Dateobj.getfullyear ()-1); } d= Math.floor ((dateobj.valueof ()-firstday.valueof ())/86400000); returnMath.floor (D/7) + 1;} //get the first day of a yearfunctionGetfirstweekbegday (year) {varTempdate =NewDate (year, 0, 1); vartemp =Tempdate.getday (); if(Temp = = 1) {returntempdate; } temp= Temp = = 0 7: temp; Tempdate= Tempdate.setdate (Tempdate.getdate () + (8-temp)); return NewDate (tempdate);}

JS gets a week, month, next month, a quarter of the start date, the end date and the judgment date of the week

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.