JS获取周、月、季度日期

效果:

代码:

  1 //用于获取日期本周、本月、本季度的js
  2 //Author : guanghe
  3 //文件引用方法:<script src="${staticPath}/common/js/dateUtil.js"></script>
  4 /**
  5  * 获取本周、本季度、本月、上月的开始日期、结束日期
  6  */
  7 var now = new Date(); //当前日期
  8 var nowDayOfWeek = now.getDay(); //今天本周的第几天
  9 var nowDay = now.getDate(); //当前日
 10 var nowMonth = now.getMonth(); //当前月
 11 var nowYear = now.getYear(); //当前年
 12 nowYear += (nowYear < 2000) ? 1900 : 0;
 13 
 14 //格式化日期:yyyy-MM-dd
 15 function formatDate(date) {
 16     var myyear = date.getFullYear();
 17     var mymonth = date.getMonth() + 1;
 18     var myweekday = date.getDate();
 19     if (mymonth < 10) {
 20         mymonth = "0" + mymonth;
 21     }
 22     if (myweekday < 10) {
 23         myweekday = "0" + myweekday;
 24     }
 25     return (myyear + "-" + mymonth + "-" + myweekday);
 26 }
 27 
 28 //获得某月的天数
 29 function getMonthDays(theYear, theMonth) {
 30     var monthStartDate = new Date(theYear, theMonth, 1);
 31     var monthEndDate = new Date(theYear, theMonth + 1, 1);
 32     var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
 33     return days;
 34 }
 35 
 36 //获得本季度的开始月份
 37 function getQuarterStartMonth(theMonth) {
 38     if(!theMonth) {
 39         theMonth = nowMonth;
 40     }
 41     var quarterStartMonth = 0;
 42     if (theMonth < 3) {
 43         quarterStartMonth = 0;
 44     }
 45     if (2 < theMonth && theMonth < 6) {
 46         quarterStartMonth = 3;
 47     }
 48     if (5 < theMonth && theMonth < 9) {
 49         quarterStartMonth = 6;
 50     }
 51     if (theMonth > 8) {
 52         quarterStartMonth = 9;
 53     }
 54     return quarterStartMonth;
 55 }
 56 
 57 //获得本周的开始日期
 58 function ghGetWeekStartDate() {
 59     var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 1);
 60     return formatDate(weekStartDate);
 61 }
 62 //获得本周的结束日期
 63 function ghGetWeekEndDate() {
 64     var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + 1);
 65     return formatDate(weekEndDate);
 66 }
 67 
 68 //获得下周的开始日期
 69 function ghGetNextWeekStartDate() {
 70     var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 7 + 1);
 71     return formatDate(weekStartDate);
 72 }
 73 
 74 //获得本月的开始日期
 75 function ghGetMonthStartDate() {
 76     var monthStartDate = new Date(nowYear, nowMonth, 1);
 77     return formatDate(monthStartDate);
 78 }
 79 
 80 //获得本月的结束日期
 81 function ghGetMonthEndDate() {
 82     var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowYear, nowMonth));
 83     return formatDate(monthEndDate);
 84 }
 85 
 86 //获得下月开始时间
 87 function ghGetNextMonthStartDate() {
 88     var theYear = nowYear;
 89     var theMonth = nowMonth + 1;
 90     if(theMonth == 12) {
 91         theYear += 1;
 92         theMonth = 0;
 93     }
 94     var nextMonthStartDate = new Date(theYear, theMonth, 1);
 95     return formatDate(nextMonthStartDate);
 96 }
 97 
 98 //获得本季度的开始日期
 99 function ghGetQuarterStartDate() {
100     var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
101     return formatDate(quarterStartDate);
102 }
103 
104 //或的本季度的结束日期
105 function ghGetQuarterEndDate() {
106     var quarterEndMonth = getQuarterStartMonth() + 2;
107     var quarterStartDate = new Date(nowYear, quarterEndMonth,
108         getMonthDays(nowYear, quarterEndMonth));
109     return formatDate(quarterStartDate);
110 }
111 
112 function ghGetNextQuarterStartDate() {
113     var theYear = nowYear;
114     var theQuarterMonth = nowMonth + 3;
115     if(theQuarterMonth > 11) {
116         theYear += 1;
117         theQuarterMonth -= 12;
118     }
119     var quarterStartDate = new Date(theYear, getQuarterStartMonth(theQuarterMonth), 1);
120     return formatDate(quarterStartDate);
121 }
122 
123 //设置查询时间
124 function ghSetDate(areaName, inputName, id) {
125     var date = $(areaName).children(inputName);
126     var dateStart = date.eq(0);
127     var dateEnd = date.eq(1);
128     switch(id) {
129         case "cur_week" :
130             dateStart.val(ghGetWeekStartDate());
131             dateEnd.val(ghGetNextWeekStartDate());
132             break;
133         case "cur_month" :
134             dateStart.val(ghGetMonthStartDate());
135             dateEnd.val(ghGetNextMonthStartDate());
136             break;
137         case "cur_quarter" :
138             dateStart.val(ghGetQuarterStartDate());
139             dateEnd.val(ghGetNextQuarterStartDate());
140             break;
141     }
142 }
143 
144 //初始化时间区域选择控件
145 // name:时间组件选择器name
146 function ghInitWidget(name) {
147     var areaName = "#" + name + "_date";
148     var inputName = "input[name='" + name + "']";
149     var str = $("<label id='theLabel' class='control-label'><a id='cur_week'>本周</a>&emsp;<a id='cur_month'>本月</a>&emsp;<a id='cur_quarter'>本季度</a>&emsp;自定义&emsp;</label>");
150     $(areaName).before(str);
151     //点击区域按钮,高亮并搜索
152     $("#theLabel a").click(function() {
153         $("#theLabel a").css("color", "#337ab7");
154         $(this).css("color", "red");
155         ghSetDate(areaName, inputName, this.id);
156         $(":button.btn-search1").click();
157     });
158     //点击日期框,区域选择按钮取消高亮
159     $(areaName).children(inputName).click(function() {
160         $("#theLabel a").css("color", "#337ab7");
161     });
162 }
原文地址:https://www.cnblogs.com/guanghe/p/10137331.html