Skip to content

TimePicker 时间选择框

输入或选择时间的控件。

代码演示

基础用法

点击 TimePicker,然后可以在浮层中选择或者输入某一时间。

vue
<template>
  <a-space direction="vertical">
    <a-time-picker v-model:value="value" />
    <a-time-picker v-model:value="strValue" value-format="HH:mm:ss" />
  </a-space>
</template>
<script setup>
import dayjs from 'dayjs';
import { ref, watch } from 'vue';
const value = ref(dayjs('08:00:00', 'HH:mm:ss'));
const strValue = ref('09:00:00');
watch(value, () => {
  console.log(value.value);
});
watch(strValue, () => {
  console.log(strValue.value);
});
</script>

无边框

无边框样式。

vue
<template>
  <a-space direction="vertical">
    <a-time-picker :bordered="false" />
    <a-time-range-picker :bordered="false" />
  </a-space>
</template>

范围选择器

通过 TimeRangePicker 使用时间范围选择器。

vue
<template>
  <a-time-range-picker />
</template>

12 小时制

12 小时制的时间选择器,默认的 format 为 h:mm:ss a

vue
<template>
  <a-space direction="vertical">
    <a-time-picker v-model:value="value" use12-hours />
    <a-time-picker v-model:value="value" use12-hours format="h:mm:ss A" style="width: 140px" />
    <a-time-picker v-model:value="value" use12-hours format="h:mm a" />
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const value = ref();
</script>

附加内容

在 TimePicker 选择框底部显示自定义的内容。

vue
<template>
  <a-space direction="vertical">
    <a-time-picker v-model:value="value" v-model:open="open" @openChange="handleOpenChange">
      <template #renderExtraFooter="{ prefixCls }">
        <a-button size="small" type="primary" @click="handleClose">OK {{ prefixCls }}</a-button>
      </template>
    </a-time-picker>
    <a-time-picker v-model:value="value" v-model:open="open2">
      <template #renderExtraFooter>
        <a-button size="small" type="primary" @click="handleClose">OK</a-button>
      </template>
    </a-time-picker>
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const open = ref(false);
const open2 = ref(false);
const value = ref();
const handleOpenChange = openStatus => {
  console.log('open', openStatus);
  open.value = openStatus;
};
const handleClose = () => {
  open.value = false;
  open2.value = false;
};
</script>

禁用

禁用时间选择。

vue
<template>
  <a-time-picker :value="dayjs('12:08:23', 'HH:mm:ss')" disabled />
</template>
<script setup>
export {};
</script>

自定义状态

使用 status 为 DatePicker 添加状态,可选 error 或者 warning

vue
<template>
  <a-space direction="vertical" style="width: 100%">
    <a-time-picker status="error" />
    <a-time-picker status="warning" />
    <a-time-range-picker status="error" />
    <a-time-range-picker status="warning" />
  </a-space>
</template>

弹出位置

可以通过 placement 手动指定弹出的位置。



vue
<template>
  <a-radio-group v-model:value="placement">
    <a-radio-button value="topLeft">topLeft</a-radio-button>
    <a-radio-button value="topRight">topRight</a-radio-button>
    <a-radio-button value="bottomLeft">bottomLeft</a-radio-button>
    <a-radio-button value="bottomRight">bottomRight</a-radio-button>
  </a-radio-group>
  <br />
  <br />
  <a-space direction="vertical" :size="12">
    <a-time-picker v-model:value="value1" :placement="placement" />
    <a-time-range-picker v-model:value="value2" :placement="placement" />
  </a-space>
</template>
<script setup>
import { ref } from 'vue';
const placement = ref('topLeft');
const value1 = ref();
const value2 = ref();
</script>

API

参数说明类型默认值
allowClear是否展示清除按钮booleantrue
autofocus自动获取焦点booleanfalse
bordered是否有边框booleantrue
clearIcon自定义的清除图标v-slot:clearIcon-
clearText清除按钮的提示文案stringclear
disabled禁用全部操作booleanfalse
disabledTime不可选择的时间DisabledTime-
format展示的时间格式stringHH:mm:ss
getPopupContainer定义浮层的容器,默认为 body 上新建 divfunction(trigger)-
hideDisabledOptions隐藏禁止选择的选项booleanfalse
hourStep小时选项间隔number1
inputReadOnly设置输入框为只读(避免在移动设备上打开虚拟键盘)booleanfalse
minuteStep分钟选项间隔number1
open(v-model)面板是否打开booleanfalse
placeholder没有值的时候显示的内容string | [string, string]请选择时间
placement选择框弹出的位置bottomLeft bottomRight topLeft topRightbottomLeft
popupClassName弹出层类名string-
popupStyle弹出层样式对象object-
renderExtraFooter选择框底部显示自定义的内容v-slot:renderExtraFooter-
secondStep秒选项间隔number1
showNow面板是否显示“此刻”按钮boolean-
status设置校验状态'error' | 'warning'-
suffixIcon自定义的选择框后缀图标v-slot:suffixIcon-
use12Hours使用 12 小时制,为 true 时 format 默认为 h:mm:ss abooleanfalse
value(v-model)当前时间dayjs-
valueFormat可选,绑定值的格式,对 value、defaultValue 起作用。不指定则绑定值为 dayjs 对象string-

DisabledTime

ts
type DisabledTime = (now: Dayjs) => {
  disabledHours?: () => number[];
  disabledMinutes?: (selectedHour: number) => number[];
  disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
};

事件

事件名称说明回调参数
change时间发生变化的回调function(time: dayjs | string, timeString: string): void
openChange面板打开/关闭时的回调(open: boolean): void

方法

名称描述
blur()移除焦点
focus()获取焦点

TimeRangePicker

属性与 DatePicker 的 RangePicker 相同。还包含以下属性:

参数说明类型默认值
order始末时间是否自动排序booleantrue
disabledTime不可选择的时间RangeDisabledTime-

RangeDisabledTime

ts
type RangeDisabledTime = (
  now: Dayjs,
  type = 'start' | 'end',
) => {
  disabledHours?: () => number[];
  disabledMinutes?: (selectedHour: number) => number[];
  disabledSeconds?: (selectedHour: number, selectedMinute: number) => number[];
};