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