VUE中使用js完成根据起止日期获取所有的季度(源码)

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;

//判断起始月所在的季度,拼接年份,得出起始时间的年和月  例如:20211
 switch (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而不是-1
        var 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
如果你觉得有用的话,就留个赞吧!蟹蟹

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猴麦麦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值