日期选择器快速选择今天,昨天,本周,上周,本月,上月,本年,最近七天,最近三天

9 篇文章 0 订阅
3 篇文章 0 订阅
  日期选择器,年月日的,开始日期和结束日期
     <el-date-picker
              v-model="startStart"
              type="daterange"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd "
              value-format="yyyy-MM-dd"
              @change="dataChange"
              :picker-options="pickerOptions"
           ></el-date-picker>

data里放快捷键

  pickerOptions: {
        shortcuts: [
          {
            text: "今日",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "昨日",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", [date, date]);
            }
          },
          {
            text: "本周",
            onClick(picker) {
             const start = new Date();
              const end=new Date();
              const nows = start.getDay()||7  //注意周日算第一天,如果周日查询本周的话,天数是0,所有如     果是0,默认设置为7
              start.setTime(start.getTime() - 3600 * 1000 * 24 * ((nows - 1)));
              picker.$emit('pick', [start, end]);
            }
          },
          {
            text: "上周",
            onClick(picker) {
              const dataValue = new Date();
              const year = dataValue.getFullYear();
              const month = dataValue.getMonth() + 1;
              const day = dataValue.getDate();
              var thisWeekStart; //本周周一的时间
              if (dataValue.getDay() == 0) {
                //周天的情况;
                thisWeekStart =
                  new Date(year + "/" + month + "/" + day).getTime() -
                  (dataValue.getDay() + 6) * 86400000;
              } else {
                thisWeekStart =
                  new Date(year + "/" + month + "/" + day).getTime() -
                  (dataValue.getDay() - 1) * 86400000;
              }
              const prevWeekStart = thisWeekStart - 7 * 86400000; //上周周一的时间
              const prevWeekEnd = thisWeekStart - 1 * 86400000; //上周周日的时间
              const start = new Date(prevWeekStart); //开始时间
              const end = new Date(prevWeekEnd); //结束时间
              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 start = new Date();
              const end = new Date(start);
              end.setMonth(start.getMonth());
              start.setMonth(start.getMonth() - 1);
              end.setDate(0);
              start.setDate(1);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "本年",
            onClick(picker) {
              const end = new Date();
              const start = new Date().getFullYear();
              const startYear = new Date(start, 0, 1);
              picker.$emit("pick", [startYear, end]);
            }
          },
          {
            text: "最近七天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近30天",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      }

注意下本周快捷键问题,周日算第一天,如果周日查询本周的话,现在的天数是0,所以默认设置为7

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python的datetime实现本周上周本月上月起止日期的代码: ```python import datetime # 本周起止日期 now = datetime.datetime.now() start_of_week = now - datetime.timedelta(days=now.weekday()) end_of_week = start_of_week + datetime.timedelta(days=6) # 上周起止日期 start_of_last_week = start_of_week - datetime.timedelta(days=7) end_of_last_week = end_of_week - datetime.timedelta(days=7) # 本月起止日期 start_of_month = datetime.datetime(now.year, now.month, 1) if now.month == 12: end_of_month = datetime.datetime(now.year+1, 1, 1) - datetime.timedelta(days=1) else: end_of_month = datetime.datetime(now.year, now.month+1, 1) - datetime.timedelta(days=1) # 上月起止日期 last_month = now.month - 1 if now.month > 1 else 12 last_year = now.year - 1 if last_month == 12 else now.year start_of_last_month = datetime.datetime(last_year, last_month, 1) end_of_last_month = datetime.datetime(now.year, now.month, 1) - datetime.timedelta(days=1) # 输出起止日期 print("本周起止日期:", start_of_week.date(), "-", end_of_week.date()) print("上周起止日期:", start_of_last_week.date(), "-", end_of_last_week.date()) print("本月起止日期:", start_of_month.date(), "-", end_of_month.date()) print("上月起止日期:", start_of_last_month.date(), "-", end_of_last_month.date()) ``` 其中关键的方法是`datetime.timedelta()`表示时间差,例如`datetime.timedelta(days=7)`表示7天时间差。`datetime.datetime()`则表示一个具体的日期时间。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值