Javascript get this week, this month, this season, this year, last month, last week, last quarter, last year, last two weeks, last month
Function datechange (name) {var begintimeobject = document. getelementbyid ("begintime"); var endtimeobject = document. getelementbyid ("endtime"); var begintime; var endtime; var now = new date (); var month = now. getmonth (); var year = now. getfullyear (); var day = now. getdate (); var wday = now. getday (); Switch (name) {Case "Tweek": // var day = now this week. getday (); now. setdate (now. getdate ()-(day-1); begintime = formatdate (now); now. setdate (now. getdate () + 6); endtime = formatdate (now); break; Case "tmonth": // This month now. setdate (1); begintime = formatdate (now); var days = getdays (now); now. setdate (days); endtime = formatdate (now); break; Case "tquarter": // var M = now this season. getmonth () + 1; var q = parseint (m + 2)/3); // obtain the quarter M = Q * 3-2; // get the first month of the season now. setmonth (m-1); now. setdate (1); begintime = formatdate (now); now. setmonth (now. getmonth () + 3); now. setdate (0); endtime = formatdate (now); break; Case "tyear": // now this year. setmonth (0); now. setdate (1); begintime = formatdate (now); now. setmonth (11); now. setdate (31); endtime = formatdate (now); break; Case "today": // Today begintime = formatdate (now); endtime = begintime; break; Case "pweek ": // last week var day = now. getday (); now. setdate (now. getdate ()-(day-1)-7); begintime = formatdate (now); now. setdate (now. getdate () + 6); endtime = formatdate (now); break; Case "pmonth": // last month now. setdate (1); now. setmonth (now. getmonth ()-1); begintime = formatdate (now); var days = getdays (now); now. setdate (days); endtime = formatdate (now); break; Case "pquarter": // var M = now in the previous quarter. getmonth () + 1; var q = parseint (m + 2)/3); // obtain the quarter M = Q * 3-2; // get the first month of the quarter M = m-3; // The last season now. setmonth (m-1); now. setdate (1); begintime = formatdate (now); now. setmonth (now. getmonth () + 3); now. setdate (0); endtime = formatdate (now); break; Case "pyear": // last year now. setfullyear (now. getfullyear ()-1); now. setmonth (0); now. setdate (1); begintime = formatdate (now); now. setmonth (11); now. setdate (31); endtime = formatdate (now); break; Case "p2week": // var day = now in the last two weeks. getday (); now. setdate (now. getdate ()-(day-1)-7*2); begintime = formatdate (now); now. setdate (now. getdate () + 6 + 7); endtime = formatdate (now); break; Case "p2month": // last month now. setdate (1); now. setmonth (now. getmonth ()-1*2); begintime = formatdate (now); now. setmonth (now. getmonth () + 2); now. setdate (0); endtime = formatdate (now); break; Case "customize": // custom begintime = ""; endtime = ""; break;} begintimeobject. value = begintime; endtimeobject. value = endtime ;}
From: http://hi.baidu.com/xixitie/item/b3d98eeca9d9633987d9de66