Skip to content

DatetimePicker

Encapsulated component for DatetimePickerView, built-in date time column building and popup interaction.

Component Type

Basic Usage

v-model binds current value, click confirm to return result through confirm.

html
<wd-datetime-picker v-model="value" v-model:visible="show" @confirm="handleConfirm" />
ts
const value = ref<number>(Date.now())
const show = ref(false)
const handleConfirm = ({ value }: { value: number | string | Array<number | string> }) => {
  console.log(value)
}

Limit Selectable Time Range

Use min-date and max-date to constrain date range.

html
<wd-datetime-picker v-model="value" v-model:visible="show" :min-date="minDate" :max-date="maxDate" />
ts
const value = ref<number>(Date.now())
const show = ref(false)
const minDate = new Date(new Date().getFullYear(), 0, 1).getTime()
const maxDate = new Date(new Date().getFullYear() + 1, 11, 31, 23, 59, 59).getTime()

Component State

Pre-confirm Validation

Set before-confirm, execute interception check when clicking confirm, supports returning boolean or Promise<boolean>.

html
<wd-datetime-picker v-model="value" v-model:visible="show" :before-confirm="beforeConfirm" />
ts
const value = ref<number>(Date.now())
const show = ref(false)
const beforeConfirm = (value: number | string | Array<number | string>) => {
  if (Array.isArray(value)) return true
  if (typeof value === 'number') return value <= Date.now()
  return true
}

Component Variant

Type Switch

Supports datetime, date, year-month, year, time five types.

html
<wd-datetime-picker v-model="dateValue" type="date" />
<wd-datetime-picker v-model="yearMonthValue" type="year-month" />
<wd-datetime-picker v-model="yearValue" type="year" />
<wd-datetime-picker v-model="timeValue" type="time" />
<wd-datetime-picker v-model="datetimeValue" type="datetime" />
ts
const dateValue = ref<number>(Date.now())
const yearMonthValue = ref<number>(Date.now())
const yearValue = ref<number>(Date.now())
const timeValue = ref<string>('09:20')
const datetimeValue = ref<number>(Date.now())

Enable Second Selection

In time and datetime types, seconds can be displayed through use-second.

html
<wd-datetime-picker v-model="timeValue" type="time" use-second />
<wd-datetime-picker v-model="value" type="datetime" use-second />
ts
const timeValue = ref<string>('09:20:30')
const value = ref<number>(Date.now())

Range Selection

When v-model is an array, range selection mode is enabled.

html
<wd-datetime-picker v-model="rangeValue" v-model:visible="show" />
ts
const rangeValue = ref<(string | number)[]>(['', Date.now()])
const show = ref(false)

Component Style

Custom Column Item Format

Customize roller option text through formatter.

html
<wd-datetime-picker v-model="value" :formatter="formatter" />
ts
const formatter = (type: string, value: number) => {
  switch (type) {
    case 'year':
      return `${value}Year`
    case 'month':
      return `${value}Month`
    case 'date':
      return `${value}Day`
    case 'hour':
      return `${value}Hour`
    case 'minute':
      return `${value}Minute`
    case 'second':
      return `${value}Second`
    default:
      return `${value}`
  }
}

Filter Column Options

Filter optional list through filter.

html
<wd-datetime-picker v-model="value" :filter="filter" />
ts
const filter = ({ type, values }: { type: string; values: number[] }) => {
  if (type === 'minute') {
    return values.filter((value) => value % 5 === 0)
  }
  return values
}

Custom Range Tab Text

In range selection mode, format start/end label display through display-format-tab-label.

html
<wd-datetime-picker v-model="rangeValue" :display-format-tab-label="displayFormatTabLabel" />
ts
const displayFormatTabLabel = (items: Array<{ label: string | number }>) => {
  return `${items[0].label}Year${items[1].label}Month${items[2].label}Day ${items[3].label}:${items[4].label}`
}

Attributes

ParameterDescriptionTypeDefault Value
v-model / modelValueBinding value. time type is string; range mode is array; other types are timestampstring | number | Array<string | number>-
visible / v-model:visibleWhether to show popupbooleanfalse
typePicker type, optional values are datetime, date, year-month, time, yearstringdatetime
titlePopup titlestring-
cancel-button-textCancel button textstring-
confirm-button-textConfirm button textstring-
close-on-click-modalWhether to close when clicking maskbooleantrue
safe-area-inset-bottomWhether popup panel sets bottom safe distancebooleantrue
item-heightSingle item heightnumber44
visible-item-countVisible item countnumber6
value-keyOption value field namestringvalue
label-keyOption text field namestringlabel
min-dateMinimum date (timestamp)numberJanuary 1st of current year minus 10 years
max-dateMaximum date (timestamp)numberDecember 31st of current year plus 10 years 23:59:59
min-hourMinimum hour (effective for time type)number0
max-hourMaximum hour (effective for time type)number23
min-minuteMinimum minute (effective for time type)number0
max-minuteMaximum minute (effective for time type)number59
use-second v1.10.0Whether to show second selection, only effective for time and datetimebooleanfalse
min-second v1.10.0Minimum seconds, only effective for time and datetimenumber0
max-second v1.10.0Maximum seconds, only effective for time and datetimenumber59
formatterCustom roller option format functionDatetimePickerViewFormatter-
filterCustom filter functionDatetimePickerViewFilter-
before-confirmPre-confirm validation function, receives (value), returns boolean or Promise<boolean>DatetimePickerBeforeConfirm-
display-format-tab-labelCustom Tab label format function in range modeDatetimePickerDisplayFormatTabLabel-
z-indexPopup z-indexnumber15
immediate-change v1.2.25Whether to trigger change immediately when finger is released (only WeChat/Alipay Mini Program)booleanfalse
root-portal v1.11.0Whether to detach from document flow rendering (H5: teleport, App: renderjs, Mini Program: root-portal)booleanfalse
custom-classRoot node custom class namestring''
custom-styleRoot node custom stylestring''
custom-view-classpickerView external custom style classstring''

Events

Event NameDescriptionParameters
openTriggered when popup opens-
cancelTriggered when clicking cancel or closing popup-
confirmTriggered when clicking confirm button{ value }
changeTriggered when selected value changes{ value }
toggleTriggered when switching start/end Tab in range modeValue of currently active Tab

Methods

Method NameDescriptionParameters
openOpen picker popup-
closeClose picker popup-

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-datetime-picker-popup-radius$radius-large $radius-large $radius-zero $radius-zero日期时间选择器弹窗圆角
--wot-datetime-picker-title-color$text-main标题文字颜色
--wot-datetime-picker-title-height$n-56标题区域高度
--wot-datetime-picker-title-font-size$typography-title-size-main标题文字字号
--wot-datetime-picker-title-line-height$typography-title-line--height-size-main标题文字行高
--wot-datetime-picker-title-font-weight$font-weight-medium标题文字字重
--wot-datetime-picker-title-padding$padding-zero $padding-loose标题区域内边距
--wot-datetime-picker-action-font-size$typography-body-size-large操作按钮字号
--wot-datetime-picker-action-line-height$typography-body-line--height-size-large操作按钮行高
--wot-datetime-picker-action-color$primary-6操作按钮文字颜色
--wot-datetime-picker-range-padding0 $padding-loose $padding-extra-loose范围标签内边距
--wot-datetime-picker-range-font-size$typography-body-size-main范围标签字号
--wot-datetime-picker-range-item-font-size$typography-body-size-extra-large范围标签项字号
--wot-datetime-picker-range-item-font-weight$font-weight-medium范围标签项字重
--wot-datetime-picker-range-item-line-height$typography-body-line--height-size-extra-large范围标签项行高
--wot-datetime-picker-range-item-padding$spacing-loose $spacing-zero范围标签项内边距
--wot-datetime-picker-range-item-color$primary-6范围标签项颜色
--wot-datetime-picker-range-item-placeholder-color$text-placeholder占位符颜色
--wot-datetime-picker-range-item-line-color$primary-6范围标签项下划线颜色
--wot-datetime-picker-range-item-line-placeholder-color$border-main范围标签项下划线占位符颜色
--wot-datetime-picker-range-separator-margin$spacing-zero $spacing-tight分隔符水平间距
--wot-datetime-picker-range-separator-color$text-auxiliary分隔符颜色
--wot-datetime-picker-range-separator-width$n-8分隔符宽度
--wot-datetime-picker-range-separator-height$stroke-main分隔符高度

Source Code

Documentation
Component

Released under the MIT License.

Released under the MIT License.