Skip to content

DatePicker 日期选择

用于选择日期,支持年月日、年月、年份等多种模式。

基础用法

vue
<template>
  <button @click="show = true">选择日期</button>
  <text>{{ value }}</text>

  <dr-date-picker
    v-model:show="show"
    title="选择日期"
    @confirm="onConfirm"
  />
</template>

<script setup>
import { ref } from 'vue'

const show = ref(false)
const value = ref('')

const onConfirm = (e) => {
  value.value = e.value // 2024-12-13
}
</script>

选择年月

设置 mode="year-month" 只选择年月。

vue
<dr-date-picker
  v-model:show="show"
  mode="year-month"
  title="选择年月"
  @confirm="onConfirm"
/>

选择年份

设置 mode="year" 只选择年份。

vue
<dr-date-picker
  v-model:show="show"
  mode="year"
  title="选择年份"
  @confirm="onConfirm"
/>

限制日期范围

通过 min-datemax-date 限制可选日期范围。

vue
<script setup>
const minDate = new Date(2024, 0, 1)   // 2024-01-01
const maxDate = new Date(2024, 11, 31) // 2024-12-31
</script>

<template>
  <dr-date-picker
    v-model:show="show"
    :min-date="minDate"
    :max-date="maxDate"
    title="选择2024年内日期"
  />
</template>

默认日期

通过 v-model 设置默认选中的日期。

vue
<dr-date-picker
  v-model:show="show"
  v-model="date"
  title="选择日期"
/>

<script setup>
const date = ref('2024-06-15')
</script>

自定义单位

通过 year-suffixmonth-suffixday-suffix 自定义年月日单位。

vue
<dr-date-picker
  v-model:show="show"
  year-suffix="年"
  month-suffix="月"
  day-suffix="日"
/>

API

Props

参数说明类型默认值
v-model:show是否显示booleanfalse
v-model当前选中日期string | Date''
mode模式:date year-month yearstringdate
min-date最小可选日期string | Date十年前
max-date最大可选日期string | Date十年后
title标题string选择日期
show-toolbar是否显示工具栏booleantrue
confirm-text确认按钮文字string确定
cancel-text取消按钮文字string取消
year-suffix年份单位string
month-suffix月份单位string
day-suffix日期单位string
mask-closable点击遮罩是否关闭booleantrue
z-index层级number5000

Events

事件名说明回调参数
confirm点击确认时触发{ value: string, date: Date }
cancel点击取消时触发-
change选择变化时触发{ value: string, date: Date }
close关闭时触发-

回调参数说明

字段说明类型示例
value格式化后的日期字符串string2024-12-13
date日期对象DateDate 实例

基于 MIT 许可发布