Back to Vant Weapp

DatetimePicker 时间选择

packages/datetime-picker/README.md

1.11.75.5 KB
Original Source

DatetimePicker 时间选择

介绍

用于选择时间,支持日期、时分等时间维度,通常与 弹出层 组件配合使用。

引入

app.jsonindex.json中引入组件,详细介绍见快速上手

json
"usingComponents": {
  "van-datetime-picker": "@vant/weapp/datetime-picker/index"
}

代码演示

选择完整时间

value 为时间戳。

html
<van-datetime-picker
  type="datetime"
  value="{{ currentDate }}"
  min-date="{{ minDate }}"
  max-date="{{ maxDate }}"
  bind:input="onInput"
/>
javascript
Page({
  data: {
    minHour: 10,
    maxHour: 20,
    minDate: new Date().getTime(),
    maxDate: new Date(2019, 10, 1).getTime(),
    currentDate: new Date().getTime(),
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
});

选择日期(年月日)

value 为时间戳,通过传入 formatter 函数对选项文字进行处理。

html
<van-datetime-picker
  type="date"
  value="{{ currentDate }}"
  bind:input="onInput"
  min-date="{{ minDate }}"
  formatter="{{ formatter }}"
/>
js
Page({
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
});

选择日期(年月)

value 为时间戳。

html
<van-datetime-picker
  type="year-month"
  value="{{ currentDate }}"
  min-date="{{ minDate }}"
  bind:input="onInput"
/>
js
Page({
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
});

选择时间

value 为字符串。

html
<van-datetime-picker
  type="time"
  value="{{ currentDate }}"
  min-hour="{{ minHour }}"
  max-hour="{{ maxHour }}"
  bind:input="onInput"
/>
js
Page({
  data: {
    currentDate: '12:00',
    minHour: 10,
    maxHour: 20,
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
});

选项过滤器

通过传入 filter 函数,可以对选项数组进行过滤,实现自定义时间间隔。

html
<van-datetime-picker
  type="time"
  value="{{ currentDate }}"
  filter="{{ filter }}"
/>
js
Page({
  data: {
    currentDate: '12:00',
    filter(type, options) {
      if (type === 'minute') {
        return options.filter((option) => option % 5 === 0);
      }

      return options;
    },
  },
});

API

Props

参数说明类型默认值
value当前选中值string | number-
type类型,可选值为 date time year-month
<strong>不建议动态修改</strong>stringdatetime
min-date可选的最小时间,精确到分钟number十年前
max-date可选的最大时间,精确到分钟number十年后
min-hour可选的最小小时,针对 time 类型number0
max-hour可选的最大小时,针对 time 类型number23
min-minute可选的最小分钟,针对 time 类型number0
max-minute可选的最大分钟,针对 time 类型number59
filter选项过滤函数(type 可能值为 year, month, day, hour, minute)(type, values) => values-
formatter选项格式化函数(type 可能值为 year, month, day, hour, minute)(type, value) => value-
title顶部栏标题string''
show-toolbar是否显示顶部栏booleantrue
loading是否显示加载状态booleanfalse
item-height选项高度number44
confirm-button-text确认按钮文字string确认
cancel-button-text取消按钮文字string取消
visible-item-count可见的选项个数number6

Events

事件名称说明回调参数
bind:input当值变化时触发的事件当前 value
bind:change当值变化时触发的事件组件实例
bind:confirm点击完成按钮时触发的事件当前 value
bind:cancel点击取消按钮时触发的事件-

change 事件

change事件中,可以获取到组件实例,对组件进行相应的更新等操作:

函数说明
getColumnValue(index)获取对应列中选中的值
setColumnValue(index, value)设置对应列中选中的值
getColumnValues(index)获取对应列中所有的备选值
setColumnValues(index, values)设置对应列中所有的备选值
getValues()获取所有列中被选中的值,返回一个数组
setValues(values)values为一个数组,设置所有列中被选中的值

外部样式类

类名说明
active-class选中项样式类
toolbar-class顶部栏样式类
column-class列样式类