js查询最近一个月,最近一周,本月,本年,上月,上年

js查询最近一个月,最近一周,本月,本年,上月,上年

 <el-select
    v-model="value"
    placeholder="请选择"
    @change="getDateInput"
  >
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    >
    </el-option>
  </el-select>

js部分

value: "",
options: [
  { value: "", label: "最近一个月" },
   {
     value: 1,
     label: "最近一周",
   },
   {
     value: 2,
     label: "本月",
   },
   {
     value: 3,
     label: "本年",
   },
   {
     value: 4,
     label: "上月",
   },
   {
     value: 5,
     label: "上年",
   },
 ],
    getLastTime(value) {
      console.log(value);
      let now = new Date(); //当前日期
      let year = now.getFullYear(); //当前年
      let month = now.getMonth() + 1; //0-11表示1-12月
      let day = now.getDate(); //当前日
      let dateObj = {};
      dateObj.now = year + "-" + month + "-" + day;
      let nowMonthDay = new Date(year, month, 0).getDate(); // 获取当年月份的最后一天
      let currentYearFirstDate = new Date(year, 0, 1); // 获取今年日期格式
      console.log(currentYearFirstDate);
      let partiYear = currentYearFirstDate.getFullYear(); // 获取今年格式
      let partiMonth = currentYearFirstDate.getMonth() + 1; // 获取月格式
      let partiDay = currentYearFirstDate.getDate(); // 获取日格式
      this.endTime = dateObj.now;
      let startMonthDay = new Date(year, parseInt(month) - 1, 0).getDate(); // 获取上一个月的月底
      if (value === undefined || value === "") {
        // 最近一月
        if (month - 1 <= 0) {
          dateObj.start = year - 1 + "-" + 12 + "-" + day;
        } else {
          if (startMonthDay < day) {
            if (day < endMonthDay) {
              dateObj.start =
                year +
                "-" +
                (month - 1) +
                "-" +
                (startMonthDay - (endMonthDay - day));
            } else {
              dateObj.start = year + "-" + (month - 1) + "-" + startMonthDay;
            }
          } else {
            dateObj.start = year + "-" + (month - 1) + "-" + day;
          }
        }
        this.startTime = dateObj.start;
      } else if (value === 1) {
        // 最近一周
        if (day - 7 <= 0) {
          if (month - 1 <= 0) {
            dateObj.start = year - 1 + "-" + 12 + "-" + (31 - (7 - day));
          } else {
            dateObj.start =
              year + "-" + (month - 1) + "-" + (startMonthDay - (7 - day));
          }
        } else {
          dateObj.start = year + "-" + month + "-" + (day - 7);
        }
        this.startTime = dateObj.start;
      } else if (value === 2) {
        dateObj.now = year + "-" + month + "-" + day;
        dateObj.start = year + "-" + month + "-1";
        this.endTime = dateObj.now;
        this.startTime = dateObj.start;
        console.log(dateObj);
      } else if (value === 3) {
        // 本年
        dateObj.start = partiYear + "-" + partiMonth + "-" + partiDay;
        this.endTime = dateObj.now;
        this.startTime = dateObj.start;
        console.log(dateObj);
      } else if (value === 4) {
        let mewMonth = now.getMonth();
        console.log(mewMonth);
        if (mewMonth === 0) {
          mewMonth = 12;
          year = year - 1;
        }
        dateObj.start = year + "-" + mewMonth + "-1";
        dateObj.end = year + "-" + month + "-" + "01"; //上个月的第一天
        console.log(dateObj);
        this.endTime = dateObj.end;
        this.startTime = dateObj.start;
      } else if (value === 5) {
        dateObj.start = partiYear - 1 + "-" + partiMonth + "-" + partiDay;
        dateObj.end = partiYear + "-" + partiMonth + "-" + partiDay;
        this.endTime = dateObj.end;
        this.startTime = dateObj.start;
      }
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值