DatePicker 实现:日期范围截止时间为23:59:59-实现逻辑

时间:2025-02-12 11:48:21
  1. 设置 value-format:首先,设置 value-format'yyyy-MM-dd HH:mm:ss' 来确保绑定的值包含时间。
  2. 监听选择事件:通过监听 change 事件,你可以在用户选择日期后修改时间部分。
  3. 调整时间:在事件处理函数中,将截止时间的时间设置为23:59:59。
<script lang="ts" setup>
import { ref } from 'vue';
import { dayjs } from 'element-plus';

const value1 = ref([])

const handleDateChange = (dataValue: Date | [Date, Date]) => {
  if (dataValue) {
    const start = new Date(dataValue[0]);
    const end = new Date(dataValue[1]);
    start.setHours(0,0,0); // 设置开始时间为当天的00:00:00点
    end.setHours(23, 59, 59); // 设置结束时间为当天的23:59:59
    value1.value[0] = dayjs(start).format('YYYY-MM-DD HH:mm:ss ');
    value1.value[1] = dayjs(end).format('YYYY-MM-DD HH:mm:ss');
  }
}
</script>

<template>
  <el-date-picker
    v-model="value1"
    type="daterange"
    unlink-panels
    range-separator=""
    start-placeholder="开始日期"
    end-placeholder="结束日期"
    @change="handleDateChange"
  />
</template>