ElementUI 周组件展示成月的第几周
组件展示
<el-date-picker unlink-panels :clearable="false" @change="weekChange":editable="false" :type="dateType":value-format="valueFormat" :format="format"v-if="this.periodFlag === '01'"v-model="queryForm.statCycle"placeholder="请选择" class="width-100"size="mini"></el-date-picker>
修改format
this.format = this.getWeekInMonth(this.queryForm.statCycle);// 根据日期判断是月的第几周getWeekInMonth(t) {if (t == undefined || t == '' || t == null) {t = new Date();} else {var _t = new Date();_t.setYear(t.getFullYear());_t.setMonth(t.getMonth());_t.setDate(t.getDate());var date = _t.getDate(); //给定的日期是几号_t.setDate(1);var d = _t.getDay(); //1. 得到当前的1号是星期几。var fisrtWeekend = d;var weekend2 = d;if (d == 0) {weekend2 = 1;fisrtWeekend = 7;//1号就是星期天} else {fisrtWeekend = 7 - d + 7; //第一周的周未是几号weekend2 = 7 - d + 1;}var str = ''if (date <= fisrtWeekend) {str = '';if(date < weekend2){//算上个月的最后一天let y = _t.getFullYear();let m = _t.getMonth();if(m == 0){m = 12;y = y - 1}let lastD = this.getLastDayOfMonth(y,m);let lastMd = lastD.getDate();let firstMd = this.getLastMonthDay(lastD);// str = 'yyyy年'+ str = y+ '年'+ m +'月第' +( 1+ Math.ceil((lastMd - firstMd) / 7))+'周'}else{str = 'yyyy年M月第1周'}return str;} else {let y = _t.getFullYear();let m = _t.getMonth();str = y+ '年'+ (m+1) +'月第'+(1 + Math.ceil((date - fisrtWeekend) / 7))+'周'return str;}}},getLastMonthDay(t){if (t == undefined || t == '' || t == null) {t = new Date();} else {var _t = new Date();_t.setYear(t.getFullYear());_t.setMonth(t.getMonth());_t.setDate(t.getDate());// var date = _t.getDate(); //给定的日期是几号_t.setDate(1);var d = _t.getDay(); //1. 得到当前的1号是星期几。var fisrtWeekend = d;if (d == 0) {fisrtWeekend = 1;//1号就是星期天} else {fisrtWeekend = 7 - d + 1; //第一周的周未是几号}return fisrtWeekend;// if (date <= fisrtWeekend) {// return 1;// } else {// return 1 + Math.ceil((date - fisrtWeekend) / 7);// }}},getLastDayOfMonth(year, month) {// 获取指定月份的下一个月的第一天const nextMonth = new Date(year, month, 1);// 将下一个月的第一天减去一天,即为指定月份的最后一天const lastDay = new Date(nextMonth - 86400000);return lastDay;//.getDate();},
自己项目记录,希望能够找到更简单的方法。