Skip to content

DatetimePicker Date/Time Picker Component

This picker is used for selecting dates and times

Note

Please run npm i dayjs first to install the dependency.

📌 Platform Compatibility

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

⚠️ Notes

Notes

  • The minDate and maxDate parameters must be passed as timestamps (in milliseconds), not date strings
  • WeChat Mini Program does not support passing function parameters via props; formatter must be set through the setFormatter method
  • When hasInput is set to true, the component comes with a built-in input box, and clicking the input box opens the picker
  • mode supports multiple formats, see the description below for details
  • minHour, maxHour, minMinute, maxMinute are only effective when mode=time

🏯 Basic Usage Examples

Open via the built-in input box

html
<template>
    <hy-datetime-picker v-model="value" has-input></hy-datetime-picker>
</template>

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

    const value = ref('');
</script>

Open via an external button

html
<template>
    <view>
        <hy-button @click="showPicker = true" :text="value || '选择日期'"></hy-button>
        <hy-datetime-picker
            v-model="value"
            v-model:show="showPicker"
            mode="date"
        ></hy-datetime-picker>
    </view>
</template>

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

    const value = ref('');
    const showPicker = ref(false);
</script>

Various time modes

  • datetime: Select date in format YYYY-MM-DD HH:mm:ss
  • date: Select date in format YYYY-MM-DD
  • time: Select time in format HH:mm
  • year-month: Select date in format YYYY-MM
  • month-day: Select date in format MM-DD
  • hour-minute: Select time in format HH:mm
  • minute-second: Select time in format mm:ss
html
<template>
    <view>
        <!-- Full datetime -->
        <hy-datetime-picker v-model="value1" has-input></hy-datetime-picker>

        <!-- Year-month-day -->
        <hy-datetime-picker v-model="value2" has-input mode="date"></hy-datetime-picker>

        <!-- Year-month -->
        <hy-datetime-picker v-model="value3" has-input mode="year-month"></hy-datetime-picker>

        <!-- Month-day -->
        <hy-datetime-picker v-model="value4" has-input mode="month-day"></hy-datetime-picker>

        <!-- Time (hour:minute:second) -->
        <hy-datetime-picker v-model="value5" has-input mode="time"></hy-datetime-picker>

        <!-- Hour/minute -->
        <hy-datetime-picker v-model="value6" has-input mode="hour-minute"></hy-datetime-picker>

        <!-- Minute/second -->
        <hy-datetime-picker v-model="value7" has-input mode="minute-second"></hy-datetime-picker>
    </view>
</template>

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

    const value1 = ref('');
    const value2 = ref('');
    const value3 = ref('');
    const value4 = ref('');
    const value5 = ref('');
    const value6 = ref('');
    const value7 = ref('');
</script>

Set maximum and minimum values

html
<template>
    <view>
        <hy-datetime-picker
            has-input
            v-model="value"
            :minDate="minDate"
            :maxDate="maxDate"
            mode="datetime"
        ></hy-datetime-picker>
    </view>
</template>

<script setup>
    import { ref, computed } from 'vue';

    const value = ref(Date.now());
    // Set the selectable range from one year in the past to one year in the future
    const minDate = computed(() => {
        return new Date(new Date().getFullYear() - 1, 0, 1).getTime();
    });
    const maxDate = computed(() => {
        return new Date(new Date().getFullYear() + 1, 11, 31).getTime();
    });
</script>

Custom time formatting

html
<template>
    <view>
        <hy-datetime-picker
            ref="datetimePickerRef"
            :show="show"
            v-model="value"
            mode="datetime"
            :formatter="formatter"
        ></hy-datetime-picker>
        <hy-button @click="show = true">打开</hy-button>
    </view>
</template>

<script setup>
    import { ref, onMounted } from 'vue';

    const show = ref(false);
    const value = ref(Date.now());
    const datetimePickerRef = ref(null);

    const formatter = (type, value) => {
        if (type === 'year') {
            return `${value}年`;
        }
        if (type === 'month') {
            return `${value}月`;
        }
        if (type === 'day') {
            return `${value}日`;
        }
        return value;
    };

    onMounted(() => {
        // WeChat Mini Program requires this syntax
        // datetimePickerRef.value.setFormatter(formatter);
    });
</script>

Custom button text and colors

html
<template>
    <hy-datetime-picker
        has-input
        v-model="value"
        cancelText="取消选择"
        confirmText="确认选择"
        cancelColor="#999999"
        confirmColor="#f56c6c"
    ></hy-datetime-picker>
</template>

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

    const value = ref('');
</script>

Custom input box style

html
<template>
    <hy-datetime-picker
        has-input
        v-model="value"
        :input="{ placeholder: '请选择日期时间', border: true }"
        customStyle="{ marginTop: '20rpx' }"
    ></hy-datetime-picker>
</template>

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

    const value = ref('');
</script>

API

DatetimePicker Props

ParameterDescriptionTypeDefault Value
showControls the popup and dismiss of the pickerbooleanfalse
v-modelBound valuestring | number | Date-
modeDisplay formatdate | datetime | time | year-month | month-day | hour-minute | minute-seconddatetime
hasInputWhether to include a built-in input boxbooleanfalse
inputCollection of input box properties, applicable when hasInput is true, see Input API for detailsHyInputProps-
formatDate format displayed in the input boxstring'YYYY-MM-DD HH:mm'
popupModeControls the popup direction of the pickerbottom | center | left | right | topbottom
showToolbarWhether to show the top toolbarbooleantrue
titleTop titlestring-
maxDateMaximum selectable time (timestamp in milliseconds)number10 years later
minDateMinimum selectable time (timestamp in milliseconds)number10 years earlier
minHourMinimum selectable hour, only effective when mode=timenumber0
maxHourMaximum selectable hour, only effective when mode=timenumber23
minMinuteMinimum selectable minute, only effective when mode=timenumber0
maxMinuteMaximum selectable minute, only effective when mode=timenumber59
filterOption filter functionfunctionnull
formatterOption format functionfunctionnull
loadingWhether to show the loading statebooleanfalse
itemHeightHeight of a single option in each columnnumber44
cancelTextText of the cancel buttonstringCancel
confirmTextText of the confirm buttonstringConfirm
cancelColorColor of the cancel buttonstring#909193
confirmColorColor of the confirm buttonstring#3c9cff
visibleItemCountNumber of visible options per columnnumber5
closeOnClickOverlayWhether to allow closing the picker by clicking the overlaybooleanfalse
defaultIndexDefault index for each columnarray-
toolbarRightSlotWhether to show the right slotbooleanfalse
customStyleCustom outer style of the input boxCSSProperties-
customClassCustom outer class namestring-

Events

Event NameDescriptionCallback Parameters
closeTriggered when the picker is closed-
confirmTriggered when the confirm button is clicked, returns the currently selected value{ value, mode }
changeTriggered when the selected value changes{ value, mode }
cancelTriggered when the cancel button is clicked-

Slots

Slot NameDescription
toolbar-rightToolbar right content, for customizing the right side; due to WeChat Mini Program limitations, toolbarRightSlot="true" must also be set for it to take effect
toolbar-bottomCustom area below the input box

Methods

Method NameDescription
setFormatterInternal method exposed for WeChat Mini Program compatibility, used to set the format function

Typings

Type Description
ts
type IParam = {
    /** Value */
    value: string | number;
    /** Time mode */
    mode: HyApp.DateModeEnum;
};
03:29