天天看点

JS获取本周、本季度、本月、上月的开始日期、结束日期

  1. /** 
  2.  * 获取本周、本季度、本月、上月的开始日期、结束日期 
  3.  */  
  4. var now = new Date();                    //当前日期     
  5. var nowDayOfWeek = now.getDay();         //今天本周的第几天     
  6. var nowDay = now.getDate();              //当前日     
  7. var nowMonth = now.getMonth();           //当前月     
  8. var nowYear = now.getYear();             //当前年     
  9. nowYear += (nowYear < 2000) ? 1900 : 0;  //    
  10. var lastMonthDate = new Date();  //上月日期  
  11. lastMonthDate.setDate(1);  
  12. lastMonthDate.setMonth(lastMonthDate.getMonth()-1);  
  13. var lastYear = lastMonthDate.getYear();  
  14. var lastMonth = lastMonthDate.getMonth();  
  15. //格式化日期:yyyy-MM-dd     
  16. function formatDate(date) {      
  17.     var myyear = date.getFullYear();     
  18.     var mymonth = date.getMonth()+1;     
  19.     var myweekday = date.getDate();      
  20.     if(mymonth < 10){     
  21.         mymonth = "0" + mymonth;     
  22.     }      
  23.     if(myweekday < 10){     
  24.         myweekday = "0" + myweekday;     
  25.     }     
  26.     return (myyear+"-"+mymonth + "-" + myweekday);      
  27. }      
  28. //获得某月的天数     
  29. function getMonthDays(myMonth){     
  30.     var monthStartDate = new Date(nowYear, myMonth, 1);      
  31.     var monthEndDate = new Date(nowYear, myMonth + 1, 1);      
  32.     var   days   =   (monthEndDate   -   monthStartDate)/(1000   *   60   *   60   *   24);      
  33.     return   days;      
  34. }     
  35. //获得本季度的开始月份     
  36. function getQuarterStartMonth(){     
  37.     var quarterStartMonth = 0;     
  38.     if(nowMonth<3){     
  39.        quarterStartMonth = 0;     
  40.     if(2<nowMonth && nowMonth<6){     
  41.        quarterStartMonth = 3;     
  42.     if(5<nowMonth && nowMonth<9){     
  43.        quarterStartMonth = 6;     
  44.     if(nowMonth>8){     
  45.        quarterStartMonth = 9;     
  46.     return quarterStartMonth;     
  47. //获得本周的开始日期     
  48. function getWeekStartDate() {      
  49.     var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);      
  50.     return formatDate(weekStartDate);     
  51. //获得本周的结束日期     
  52. function getWeekEndDate() {      
  53.     var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));      
  54.     return formatDate(weekEndDate);     
  55. //获得本月的开始日期     
  56. function getMonthStartDate(){     
  57.     var monthStartDate = new Date(nowYear, nowMonth, 1);      
  58.     return formatDate(monthStartDate);     
  59. //获得本月的结束日期     
  60. function getMonthEndDate(){     
  61.     var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));      
  62.     return formatDate(monthEndDate);     
  63. //获得上月开始时间  
  64. function getLastMonthStartDate(){  
  65.     var lastMonthStartDate = new Date(nowYear, lastMonth, 1);  
  66.     return formatDate(lastMonthStartDate);    
  67. }  
  68. //获得上月结束时间  
  69. function getLastMonthEndDate(){  
  70.     var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));  
  71.     return formatDate(lastMonthEndDate);    
  72. //获得本季度的开始日期     
  73. function getQuarterStartDate(){     
  74.     var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);      
  75.     return formatDate(quarterStartDate);     
  76. //或的本季度的结束日期     
  77. function getQuarterEndDate(){     
  78.     var quarterEndMonth = getQuarterStartMonth() + 2;     
  79.     var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));      
  80. }