首页 > 其他分享 >element-plus el-date-picker 实现周选择

element-plus el-date-picker 实现周选择

时间:2023-08-07 12:01:30浏览次数:32  
标签:picker el const dayjs element Date date getMonth getDate

在使用element-plus的时间选择器的时候,有时候会有选择一周的需求,可以使用以下方式进行实现。
首先使用type=week属性实现基本的样式

 <el-date-picker
    v-model="baseForm.startTime"
    type="week"
    placeholder="选择周"
  >
 </el-date-picker>


接下来修改其值的格式化为某日到某日的格式,需要使用format指定格式

 <el-date-picker
    v-model="baseForm.startTime"
    type="week"
    :format="baseForm.startTime + ' 至 ' + baseForm.endTime"
    placeholder="选择周"
  >
 </el-date-picker>

js代码如下

import dayjs from 'dayjs'
const baseForm = reactive({
  startTime: dayjs().format('YYYY-MM-DD'),
  endTime: dayjs().format('YYYY-MM-DD')
}

el-date-picker添加change事件

<el-date-picker
  v-model="baseForm.startTime"
  type="week"
  :format="baseForm.startTime + ' 至 ' + baseForm.endTime"
  placeholder="选择周"
  @change="changeTime"
>
</el-date-picker>

js函数实现为

// 时间戳转为yy-mm-dd
const getCurrentTime = (data, num) => {
  let date = new Date(data)
  const Y = date.getFullYear()
  const M = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  const D = date.getDate() + num < 10 ? '0' + (date.getDate() + num) : date.getDate() + num
  date = Y + '-' + M + '-' + D
  return date
}

// 选择周时间的任意一天
const changeTime = val => {
  const date = new Date(val)
  const y = date.getFullYear()
  const m = date.getMonth()
  const d = date.getDate()
  const week = date.getDay()
  const start = new Date(y, m, d - week + 1)
  const end = new Date(y, m, d - week + 7)
  baseForm.startTime = getCurrentTime(start, 0)
  baseForm.endTime = getCurrentTime(end, 0)
}

最终效果见下图

标签:picker,el,const,dayjs,element,Date,date,getMonth,getDate
From: https://www.cnblogs.com/Cxymds/p/17611043.html

相关文章

  • el-table 子级选中父级勾选 子级无一选中父级取消勾选
     //选中子级父级也选中      if(row.parentId!=0){          letparent=this.archiveTypeList.find((x=>x.id===row.parentId));          this.$refs.multipleTable.toggleRowSelection(parent,true);    ......
  • k8s 部分节点 nodelocaldns [ERROR] Failed to read node-cache coreFile /etc/coredn
      部分K8S节点nodelocaldnsCrashLoopBackOff状态报错,报错信息如下:#kubectllogsnodelocaldns-w9mgz-nkube-system2023/08/0703:18:33[INFO]UsingCorefile/etc/coredns/Corefile2023/08/0703:18:33[ERROR]Failedtoreadnode-cachecoreFile/etc/coredns/Co......
  • go 执行shell命令
    简单案例packagemainimport( "context" "fmt" "os/exec")var( ctx=context.Background())funcmain(){ cmd:=exec.CommandContext(ctx,"ip","a") res,err:=cmd.Output() iferr!=nil{ panic......
  • ps 2023 v25.0 AI功能 Intel/m1/M2电脑均可!
    Photoshop2023是由Adobe开发的一款图像编辑软件,它被广泛用于图像处理、图形设计和数字艺术创作等领域。它拥有丰富的功能和工具,可以对图像进行裁剪、调整大小、修饰、修复、添加滤镜和特效等操作,同时也支持图层、蒙版、文字处理、色彩校正、绘画等高级功能。ps最新版本:https://......
  • centos generate uuid,install libuuid-devel,compile -luuid
    //installlibuuid-develsudoyuminstalllibuuidlibuuid-devel#include<iostream>#include<stdio.h>#include<uuid/uuid.h>char*uuid_value=(char*)malloc(40);char*get_uuid(){uuid_tnew_uuid;uuid_generate(new......
  • epel在线镜像源
     前言此epel镜像源适用于红帽系列系统,如RedHatEnterprise,CentOS,AnoliOS,RockyLinux,Almalinux等linux发行版阿里云镜像源#7的镜像源[epel]name=epelbaseurl=https://mirrors.aliyun.com/epel/7/x86_64gpgcheck=0enable=1#8的镜像源[epel]name=epelbaseurl=htt......
  • epel在线镜像源
    前言此epel镜像源适用于红帽系列系统,如RedHatEnterprise,CentOS,AnoliOS,RockyLinux,Almalinux等linux发行版阿里云镜像源#7的镜像源[epel]name=epelbaseurl=https://mirrors.aliyun.com/epel/7/x86_64gpgcheck=0enable=1#8的镜像源[epel]name=epelbaseurl=https:......
  • excel 合并单元格内容数据写入
    youcanusethiscodetoopen(test.xlsx)fileandmodifyA1cellandthensaveitwithanewnameimportopenpyxlxfile=openpyxl.load_workbook('test.xlsx')sheet=xfile.get_sheet_by_name('Sheet1')sheet['A1']='hel......
  • 几个代码提升shell逼格
    在Linux系统中shell命令能够高效的运行并发的处理任务,shell脚本也并不乏味,不仅有魔音还有shell的颜色显示。下面我们就用shell中的颜色来秒提高shell脚本的逼格。我们举一个例子:echo-e"\033[44;37;5m ME \033[0mCOOL"以上命令设置背景成为蓝色,前景白色,闪烁光标,输出字符“ME”,然......
  • Ingress-nginx安装(helm)
    NginxIngress简介在Kubernetes集群中,Ingress作为集群内服务对外暴露的访问接入点,其几乎承载着集群内服务访问的所有流量。Ingress是Kubernetes中的一个资源对象,用来管理集群外部访问集群内部服务的方式。您可以通过Ingress资源来配置不同的转发规则,从而达到根据不同的规则设置访问......