element DatePicker 快捷选项 获取本周、上周、本月、上月

2023-12-15 18:55:09
 <el-date-picker
      v-model="time"
      type="daterange"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      value-format="yyyy-MM-dd"
      :picker-options="pickerOptions">
</el-date-picker>
    pickerOptions: {
        shortcuts: [
          {
            text: '本周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              console.log(start.getDay(), start.getDate(), 'start')
              var thisDay = start.getDay() - 1;
              var thisDate = start.getDate();
              if (thisDay != 0) {
                start.setDate(thisDate - thisDay);
              }
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '上周',
            onClick(picker) {
              const oDate = new Date();
              oDate.setTime(oDate.getTime() - 3600 * 1000 * 24 * 7);

              var day = oDate.getDay() - 1

              var start = new Date(),
                end = new Date();
              if (day == 0) {
                start.setDate(oDate.getDate());
                end.setDate(oDate.getDate() + 6);
              } else {
                start.setTime(oDate.getTime() - 3600 * 1000 * 24 * day);
                end.setTime(oDate.getTime() + 3600 * 1000 * 24 * (6 - day));
              }
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '本月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setDate(1);
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: '上月',
            onClick(picker) {
              const oDate = new Date();
              var year = oDate.getFullYear();
              var month = oDate.getMonth();
              var start, end;
              if (month == 0) {
                year--
                start = new Date(year, 11, 1)
                end = new Date(year, 11, 31)
              } else {
                start = new Date(year, month - 1, 1)
                end = new Date(year, month, 0);
              }

              picker.$emit('pick', [start, end]);
            }
          }
        ],
        disabledDate: (time) => {
          return time.getTime() > Date.now()//开始时间不选时,结束时间最大值小于等于当天
        }
      },

文章来源:https://blog.csdn.net/weixin_43923808/article/details/135022829
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。