- /**
- * Get start date, end date for this week, quarter, month, 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 (); //When the year before
- Nowyear + = (Nowyear < 2000)? 1900:0; //
- var lastmonthdate = new Date (); //Last month date
- Lastmonthdate.setdate (1);
- Lastmonthdate.setmonth (Lastmonthdate.getmonth ()-1);
- var lastyear = Lastmonthdate.getyear ();
- var lastmonth = Lastmonthdate.getmonth ();
- Formatted Date: YYYY-MM-DD
- function FormatDate (date) {
- var myyear = Date.getfullyear ();
- var mymonth = Date.getmonth () +1;
- var myweekday = Date.getdate ();
- if (Mymonth <) {
- Mymonth = "0" + mymonth;
- }
- if (Myweekday <) {
- Myweekday = "0" + myweekday;
- }
- return (myyear+"-" +mymonth + "-" + myweekday);
- }
- Get 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;
- }
- Get the start month of the 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;
- }
- Get the start date for this week
- function Getweekstartdate () {
- var weekstartdate = New Date (Nowyear, Nowmonth, Nowday-nowdayofweek);
- return FormatDate (weekstartdate);
- }
- Get the end date for this week
- function Getweekenddate () {
- var weekenddate = New Date (Nowyear, Nowmonth, Nowday + (6-nowdayofweek));
- return FormatDate (weekenddate);
- }
- Get the start date for this month
- function Getmonthstartdate () {
- var monthstartdate = New Date (Nowyear, Nowmonth, 1);
- return FormatDate (monthstartdate);
- }
- Get the end date for this month
- function Getmonthenddate () {
- var monthenddate = New Date (Nowyear, Nowmonth, Getmonthdays (Nowmonth));
- return FormatDate (monthenddate);
- }
- Get start time last month
- function Getlastmonthstartdate () {
- var lastmonthstartdate = New Date (Nowyear, Lastmonth, 1);
- return FormatDate (lastmonthstartdate);
- }
- Get last month end time
- function Getlastmonthenddate () {
- var lastmonthenddate = New Date (Nowyear, Lastmonth, Getmonthdays (Lastmonth));
- return FormatDate (lastmonthenddate);
- }
- Get the start date for the quarter
- function Getquarterstartdate () {
- var quarterstartdate = New Date (Nowyear, Getquarterstartmonth (), 1);
- return FormatDate (quarterstartdate);
- }
- Or the end date of the quarter
- function Getquarterenddate () {
- var quarterendmonth = getquarterstartmonth () + 2;
- var quarterstartdate = New Date (Nowyear, Quarterendmonth, Getmonthdays (Quarterendmonth));
- return FormatDate (quarterstartdate);
- }
JS get this week, this quarter, this month, the last month's start date, end date