VUE中使用js完成根据起止日期获取所有的季度(源码)
在做报表的有时会根据控件的起止日期来算每一个季度的数据

这个是控件取出来的样子

我们要把起始时间的前面的日期取出来和截至日期的后面的日期取出来进行格式化,获取到起止时间
Firstyear = this.defaultStValue.substr(0, 4);
Firstmonth = this.defaultStValue.substr(4, 2);
Lastyear = this.defaultEtValue.substr(7, 4);
Lastmonth = this.defaultEtValue.substr(11, 2);//调用方法获取起始时间对的第一天和截至时间的最后一天
var FirstDay = this.GetFirstDay(Firstyear, Firstmonth);
var LastDay = this.GetLastDay(Lastyear, Lastmonth);//根据起止时间算季度差var first;var last;//判断起始月所在的季度,拼接年份,得出起始时间的年和月 例如:20211switch (parseInt(Firstmonth)) {case 01:case 02:case 03:first = (Firstyear + "1");break;case 04:case 05:case 06:first = (Firstyear + "2");break;case 07:case 08:case 09:first = (Firstyear + "3");break;case 10:case 11:case 12:first = (Firstyear + "4");break;default:break;}//同上,获取截止时间的季度switch (parseInt(Lastmonth)) {case 01:case 02:case 03:last = (this.Lastyear + "1");break;case 04:case 05:case 06:last = (this.Lastyear + "2");break;case 07:case 08:case 09:last = (this.Lastyear + "3");break;case 10:case 11:case 12:last = (this.Lastyear + "4");break;default:break;}this.timeArray = [];var sdhu = true;while (sdhu) {//判断起始季度和截至季度是不是相等,相等则拼接最后一个季度跳出if (first == last) {switch (first.slice(4)) {case "1":this.timeArrayLegend.push("Q1 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-01-01", lasttime: first.slice(0, 4) + "-03-31", label: first.slice(0, 4) + "年第一季度", cols: [] });break;case "2":this.timeArrayLegend.push("Q2 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-04-01", lasttime: first.slice(0, 4) + "-06-30", label: first.slice(0, 4) + "年第二季度", cols: [] });break;case "3":this.timeArrayLegend.push("Q3 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-07-01", lasttime: first.slice(0, 4) + "-09-30", label: first.slice(0, 4) + "年第三季度", cols: [] });break;case "4":this.timeArrayLegend.push("Q4 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-10-01", lasttime: first.slice(0, 4) + "-12-31", label: first.slice(0, 4) + "年第四季度", cols: [] });break;default:break;}break;} else if (first != last) {switch (first.slice(4)) {case "1":this.timeArrayLegend.push("Q1 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-01-01", lasttime: first.slice(0, 4) + "-03-31", label: first.slice(0, 4) + "年第一季度", cols: [] });first = (first.slice(0, 4) + "2");break;case "2":this.timeArrayLegend.push("Q2 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-04-01", lasttime: first.slice(0, 4) + "-06-30", label: first.slice(0, 4) + "年第二季度", cols: [] });first = (first.slice(0, 4) + "3");break;case "3":this.timeArrayLegend.push("Q3 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-07-01", lasttime: first.slice(0, 4) + "-09-30", label: first.slice(0, 4) + "年第三季度", cols: [] });first = (first.slice(0, 4) + "4");break;case "4":this.timeArrayLegend.push("Q4 " + first.slice(2, 4));this.timeArraySelect.push({ firsttime: first.slice(0, 4) + "-10-01", lasttime: first.slice(0, 4) + "-12-31", label: first.slice(0, 4) + "年第四季度", cols: [] });first = ((parseInt(first.slice(0, 4)) + 1) + "1");break;default:sdhu = false;break;}continue;}}
//获取本月第一天GetFirstDay: function (year, month) {if (year != '' && month != '') {return year + "-" + month + "-01";}},
//获取本月最后一天
GetLastDay: function (year, month) {if (year != '' && month != '') {var firstDay = new Date(year, month - 1, 1) //这个月的第一天var currentMonth = firstDay.getMonth() //取得月份数var lastDay = new Date(firstDay.getFullYear(), currentMonth + 1, 0) //是0而不是-1var month = (lastDay.getMonth() + 1) + "";var day = lastDay.getDate() + "";if (month.length <= 1) {month = "0" + month;}if (day.length <= 1) {day = "0" + day;}return lastDay.getFullYear() + "-" + month + "-" + day;}
},
同事看了我的代码,吐槽…
提供了另一种思路,大家可以试一试
使用截至时间-起始时间 得出相差的时间然后除以季度
就得出了一共相差了多少个季度
然后用起始时间的年和季度一直循环相差的季度相加,
就得出了所有的季度
给颜爷爆赞!
到此就结束啦,快去练习一下吧!欢迎大佬和小Monkey沟通。

感谢大佬指正 小Monkey
如果你觉得有用的话,就留个赞吧!蟹蟹