0
点赞
收藏
分享

微信扫一扫

ElementPlus-日期选择器实现周选择

迎月兮 2024-06-02 阅读 30

ElementPlus的日期选择器实现周选择,并且显示的是日期范围,其效果如下:
在这里插入图片描述

  1. 首先修改中文语言环境,否则日期选择器是从周日开始的。在main.js文件中加上以下代码:
import ElementPlus,{dayjs as elDayjs} from 'element-plus';
import zhCn from "element-plus/es/locale/lang/zh-cn";
elDayjs.locale("zh-cn");
import 'dayjs/locale/zh-cn'

app.use(ElementPlus, {
  locale: zhCn
});
  1. 周选择的代码片段如下
          <el-date-picker
              v-model="date"
              type="week"
              :format="`${startTime} - ${endTime}`"
              placeholder="请选择周"
              @change="handleWeekChange"
          />

js部分的代码如下:

const date = ref();
const startTime = ref();
const endTime = ref();

const handleWeekChange = (value) => {
  if (value) {
    const start = proxy.$dayjs(value).startOf('week').format('YYYY-MM-DD');
    const end = proxy.$dayjs(value).endOf('week').format('YYYY-MM-DD');
    console.log(`周开始时间: ${start}`);
    console.log(`周结束时间: ${end}`);
    startTime.value = start;
    endTime.value = end;
  }
};

备注:
$dayjs方法的使用需要在main.js文件中引入

import { createApp } from 'vue';
// dayjs
import dayjs from 'dayjs';

const app = createApp(App);
app.config.globalProperties.$dayjs = dayjs;
举报

相关推荐

0 条评论