Skip to content

DatePicker 日期选择器

用于选择或输入日期。

基础用法

通过点击输入框,在弹出的日期面板中选择日期。

📅
选择的日期:
vue
<template>
  <x-date-picker v-model="value" placeholder="请选择日期" />
</template>

<script setup>
import { ref } from 'vue'
const value = ref('')
</script>

日期格式

使用 format 指定输入框的展示格式,使用 value-format 指定绑定值的格式。

📅
vue
<template>
  <x-date-picker v-model="value" format="YYYY年MM月DD日" placeholder="请选择日期" />
</template>

日期范围选择

设置 typedaterange 可以选择日期范围。

📅
vue
<template>
  <x-date-picker
    v-model="value"
    type="daterange"
    start-placeholder="开始日期"
    end-placeholder="结束日期"
  />
</template>

<script setup>
import { ref } from 'vue'
const value = ref(['', ''])
</script>

禁用日期

通过 disabledDate 函数可以禁用指定的日期。

📅
vue
<template>
  <x-date-picker v-model="value" placeholder="只能选择今天及之前的日期" :disabled-date="disabledDate" />
</template>

<script setup>
import { ref } from 'vue'
const value = ref('')

// 禁用今天之后的日期
const disabledDate = (date) => {
  const today = new Date()
  today.setHours(0, 0, 0, 0)
  return date > today
}
</script>

可清空

使用 clearable 属性即可得到一个可清空的日期选择器。

📅
vue
<template>
  <x-date-picker v-model="value" clearable placeholder="可清空" />
</template>

禁用状态

📅
vue
<template>
  <x-date-picker disabled placeholder="禁用状态" />
</template>

不同尺寸

📅
📅
📅
vue
<template>
  <x-date-picker size="large" placeholder="大型" />
  <x-date-picker placeholder="默认" />
  <x-date-picker size="small" placeholder="小型" />
</template>

API

Props

属性说明类型默认值
modelValue绑定值Date | string | number | [Date | string | number, Date | string | number]
type选择类型'date' | 'daterange' | 'month' | 'year''date'
placeholder占位文本string'请选择日期'
startPlaceholder开始日期占位string'开始日期'
endPlaceholder结束日期占位string'结束日期'
disabled是否禁用booleanfalse
readonly是否只读booleanfalse
clearable是否可清空booleanfalse
size输入框尺寸'large' | 'default' | 'small''default'
format显示格式string'YYYY-MM-DD'
valueFormat绑定值格式string
disabledDate禁用日期函数(date: Date) => boolean
minDate最小可选日期Date | string
maxDate最大可选日期Date | string

Events

事件名说明回调参数
update:modelValue值改变时触发(value: Date | string | [Date | string, Date | string] | null)
change值改变时触发(value: Date | string | [Date | string, Date | string] | null)
focus获得焦点时触发(event: FocusEvent)
blur失去焦点时触发(event: FocusEvent)
clear清空时触发

格式说明

格式说明示例
YYYY年份2024
YY年份(两位)24
MM月份(补零)01-12
M月份1-12
DD日期(补零)01-31
D日期1-31

基于 MIT 许可发布