码迷,mamicode.com
首页 > Web开发 > 详细

js获取本月,本季度,上个季度,本周,上周的起始和结束时间

时间:2015-05-27 17:15:47      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

  1 /* 获得某月的天数 */
  2     function getMonthDays(myMonth) {
  3         var nowYear = new Date().getFullYear(); //当前年
  4         var monthStartDate = new Date(nowYear, myMonth, 1);
  5         var monthEndDate = new Date(nowYear, myMonth + 1, 1);
  6         var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  7         return days;
  8     }
  9 
 10     /* 获得本周的开始日期 */
 11     function getWeekStartDate() {
 12         var now = new Date();
 13         var nowYear = now.getFullYear(); //当前年
 14         var nowMonth = now.getMonth();
 15         var nowDay = now.getDate();
 16         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 17         var day = nowDay - nowDayOfWeek;
 18         console.log("getWeekStartDate:nowYear=" + nowYear + ",nowMonth=" + nowMonth + ",nowDay=" + nowDay + ",nowDayOfWeek=" + nowDayOfWeek + ",day=" + day);
 19         console.log("getWeekStartDate:" + new Date(nowYear, nowMonth, day));
 20         return new Date(nowYear, nowMonth, day);
 21     }
 22 
 23     /* 获得本周的结束日期 */
 24     function getWeekEndDate() {
 25         var now = new Date();
 26         var nowYear = now.getFullYear(); //当前年
 27         var nowMonth = now.getMonth();
 28         var nowDay = now.getDate();
 29         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 30         var day = nowDay + (6 - nowDayOfWeek);
 31         console.log("getWeekEndDate:nowYear=" + nowYear + ",nowMonth=" + nowMonth + ",nowDay=" + nowDay + ",nowDayOfWeek=" + nowDayOfWeek + ",day=" + day);
 32         console.log("getWeekEndDate:" + new Date(nowYear, nowMonth, day));
 33         return new Date(nowYear, nowMonth, day);
 34     }
 35 
 36     /* 获得上周的开始日期 */
 37     function getLastWeekStartDate() {
 38         var now = new Date();
 39         var nowYear = now.getFullYear(); //当前年
 40         var nowMonth = now.getMonth();
 41         var nowDay = now.getDate();
 42         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 43         return new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);
 44     }
 45 
 46     /* 获得上周的结束日期 */
 47     function getLastWeekEndDate() {
 48         var now = new Date();
 49         var nowYear = now.getFullYear(); //当前年
 50         var nowMonth = now.getMonth();
 51         var nowDay = now.getDate();
 52         var nowDayOfWeek = now.getDay(); //今天本周的第几天
 53         return new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1);
 54     }
 55     /* 获得本月的开始日期 */
 56     function getMonthStartDate() {
 57         var now = new Date();
 58         var nowYear = now.getFullYear(); //当前年
 59         var nowMonth = now.getMonth();
 60         return new Date(nowYear, nowMonth, 1);
 61     }
 62 
 63     /* 获得本月的结束日期 */
 64     function getMonthEndDate() {
 65         var now = new Date();
 66         var nowYear = now.getFullYear(); //当前年
 67         var nowMonth = now.getMonth();
 68         return new Date(nowYear, nowMonth, getMonthDays(nowMonth));
 69     }
 70 
 71     /* 获得上月开始时间 */
 72     function getLastMonthStartDate() {
 73         var lastMonthDate = new Date(); //上月日期
 74         lastMonthDate.setDate(1);
 75         lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
 76         return lastMonthDate;
 77     }
 78     /* 获得上月结束时间 */
 79     function getLastMonthEndDate() {
 80         var lastMonthDate = new Date(); //上月日期
 81         lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
 82         var lastMonth = lastMonthDate.getMonth();
 83         lastMonthDate.setDate(getMonthDays(lastMonth));
 84         return lastMonthDate;
 85     }
 86 
 87     /* 获得本季度的开始月份 */
 88     function getQuarterStartMonth() {
 89         var nowMonth = new Date().getMonth();
 90         var quarterStartMonth = 0;
 91         if (nowMonth < 3) {
 92             quarterStartMonth = 0;
 93         }
 94         if (2 < nowMonth && nowMonth < 6) {
 95             quarterStartMonth = 3;
 96         }
 97         if (5 < nowMonth && nowMonth < 9) {
 98             quarterStartMonth = 6;
 99         }
100         if (nowMonth > 8) {
101             quarterStartMonth = 9;
102         }
103         return quarterStartMonth;
104     }
105 
106     /* 获得本季度的开始日期 */
107     function getQuarterStartDate() {
108         var now = new Date();
109         var nowYear = now.getFullYear(); //当前年
110         return new Date(nowYear, getQuarterStartMonth(), 1);
111     }
112     /* 获得本季度的结束日期 */
113     function getQuarterEndDate() {
114         var now = new Date();
115         var nowYear = now.getFullYear(); //当前年
116         var quarterEndMonth = getQuarterStartMonth() + 2;
117         return new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
118     }
119 
120     /* 获得上个季度的开始日期 */
121     function getLastQuarterStartDate() {
122         var now = new Date();
123         var nowYear = now.getFullYear(); //当前年
124         return new Date(nowYear, getQuarterStartMonth() - 3, 1);
125     }
126 
127     /* 获得上个季度的结束日期 */
128     function getLastQuarterEndDate() {
129         var quarterEndMonth = getQuarterStartMonth() - 1;
130         var now = new Date();
131         now.setMonth(quarterEndMonth);
132         var nowMonth = now.getMonth();
133         var nowYear = now.getFullYear();
134         return new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
135     }

 

UTC时间格式:

 2015-05-23T16:00:00.000Z
 
GMT时间格式:
 Sun May 24 2015 00:00:00 GMT+0800 (中国标准时间)

js获取本月,本季度,上个季度,本周,上周的起始和结束时间

标签:

原文地址:http://www.cnblogs.com/b302/p/4533897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!