import{o as a,a as n,y as t}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=t(`

DatetimePicker

Intro

Used to select time, support date and time dimensions, usually used with the Popup component.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { DatetimePicker } from 'vant';

const app = createApp();
app.use(DatetimePicker);

Usage

Choose Date

<van-datetime-picker
  v-model="currentDate"
  type="date"
  title="Choose Date"
  :min-date="minDate"
  :max-date="maxDate"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date(2021, 0, 17));
    return {
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate,
    };
  },
};

Choose Year-Month

<van-datetime-picker
  v-model="currentDate"
  type="year-month"
  title="Choose Year-Month"
  :min-date="minDate"
  :max-date="maxDate"
  :formatter="formatter"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date(2020, 0, 1));

    const formatter = (type, val) => {
      if (type === 'year') {
        return \`\${val} Year\`;
      }
      if (type === 'month') {
        return \`\${val} Month\`;
      }
      return val;
    };

    return {
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      formatter,
      currentDate,
    };
  },
};

Choose Month-Day

<van-datetime-picker
  v-model="currentDate"
  type="month-day"
  title="Choose Month-Day"
  :min-date="minDate"
  :max-date="maxDate"
  :formatter="formatter"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date(2020, 0, 1));

    const formatter = (type, val) => {
      if (type === 'month') {
        return \`\${val} Month\`;
      }
      if (type === 'day') {
        return \`\${val} Day\`;
      }
      return val;
    };

    return {
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      formatter,
      currentDate,
    };
  },
};

Choose Time

<van-datetime-picker
  v-model="currentTime"
  type="time"
  title="Choose Time"
  :min-hour="10"
  :max-hour="20"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref('12:00');
    return { currentTime };
  },
};

Choose DateTime

<van-datetime-picker
  v-model="currentDate"
  type="datetime"
  title="Choose DateTime"
  :min-date="minDate"
  :max-date="maxDate"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date(2020, 0, 1));
    return {
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate,
    };
  },
};

Choose DateHour

<van-datetime-picker
  v-model="currentDate"
  type="datehour"
  title="Choose DateTime"
  :min-date="minDate"
  :max-date="maxDate"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date(2020, 0, 1));
    return {
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate,
    };
  },
};

Option Filter

<van-datetime-picker
  v-model="currentTime"
  type="time"
  title="Option Filter"
  :filter="filter"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentTime = ref('12:00');

    const filter = (type, options) => {
      if (type === 'minute') {
        return options.filter((option) => Number(option) % 5 === 0);
      }
      return options;
    };

    return {
      filter,
      currentTime,
    };
  },
};

Columns Order

<van-datetime-picker
  v-model="currentDate"
  type="date"
  title="Columns Order"
  :columns-order="['month', 'day', 'year']"
  :formatter="formatter"
/>
import { ref } from 'vue';

export default {
  setup() {
    const currentDate = ref(new Date(2020, 0, 1));

    const formatter = (type, val) => {
      if (type === 'year') {
        return val + ' Year';
      }
      if (type === 'month') {
        return val + ' Month';
      }
      if (type === 'day') {
        return val + ' Day';
      }
      return val;
    };

    return {
      formatter,
      currentDate,
    };
  },
};

API

Props

AttributeDescriptionTypeDefault
typeCan be set to date time
year-month month-day datehour
stringdatetime
titleToolbar titlestring''
confirm-button-textText of confirm buttonstringConfirm
cancel-button-textText of cancel buttonstringCancel
show-toolbarWhether to show toolbarbooleantrue
loadingWhether to show loading promptbooleanfalse
readonlyWhether to be readonlybooleanfalse
filterOption filter(type: string, values: string[]) => string[]-
formatterOption text formatter(type: string, value: string) => string-
columns-orderArray for ordering columns, where item can be set to
year, month, day, hour and minute
string[]-
item-heightOption height, supports px vw vh rem unit, default pxnumber | string44
visible-item-countCount of visible columnsnumber | string6
swipe-durationDuration of the momentum animation, unit msnumber | string1000

DatePicker Props

Following props are supported when the type is date or datetime

AttributeDescriptionTypeDefault
min-dateMin dateDateTen years ago on January 1
max-dateMax dateDateTen years later on December 31

TimePicker Props

Following props are supported when the type is time

AttributeDescriptionTypeDefault
min-hourMin hour for time typenumber | string0
max-hourMax hour for time typenumber | string23
min-minuteMax minute for time typenumber | string0
max-minuteMax minute for time typenumber | string59

Events

EventDescriptionArguments
changeEmitted when value changedvalue: current value
confirmEmitted when the confirm button is clickedvalue: current value
cancelEmitted when the cancel button is clicked-

Slots

NameDescriptionSlotProps
defaultCustom toolbar content-
titleCustom title-
confirmCustom confirm button text-
cancelCustom cancel button text-
optionCustom option contentoption: string | object
columns-topCustom content above columns-
columns-bottomCustom content below columns-

Methods

Use ref to get DatetimePicker instance and call instance methods.

NameDescriptionAttributeReturn value
getPickerget Picker instance--

Types

The component exports the following type definitions:

import type {
  DatetimePickerType,
  DatetimePickerProps,
  DatetimePickerInstance,
} from 'vant';

DatetimePickerInstance is the type of component instance:

import { ref } from 'vue';
import type { DatetimePickerInstance } from 'vant';

const datetimePickerRef = ref<DatetimePickerInstance>();

datetimePickerRef.value?.getPicker();
`,20),p=[l],i={__name:"README",setup(r,{expose:s}){return s({frontmatter:{}}),(o,d)=>(a(),n("div",e,p))}};export{i as default};