js获得本周,本月,本季度的开始日期和结束日期

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

 

posted @ 2015-11-25 16:33  blackatall  阅读(277)  评论(0编辑  收藏  举报