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

Calendar

Intro

Calendar component for selecting dates or date ranges.

Install

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

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

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

Usage

Select Single Date

The confirm event will be emitted after the date selection is completed.

<van-cell title="Select Single Date" :value="date" @click="show = true" />
<van-calendar v-model:show="show" @confirm="onConfirm" />
import { ref } from 'vue';

export default {
  setup() {
    const date = ref('');
    const show = ref(false);

    const formatDate = (date) => \`\${date.getMonth() + 1}/\${date.getDate()}\`;
    const onConfirm = (value) => {
      show.value = false;
      date.value = formatDate(value);
    };

    return {
      date,
      show,
      onConfirm,
    };
  },
};

Select Multiple Date

<van-cell title="Select Multiple Date" :value="text" @click="show = true" />
<van-calendar v-model:show="show" type="multiple" @confirm="onConfirm" />
import { ref } from 'vue';

export default {
  setup() {
    const text = ref('');
    const show = ref(false);

    const onConfirm = (dates) => {
      show.value = false;
      text.value = \`\u9009\u62E9\u4E86 \${dates.length} \u4E2A\u65E5\u671F\`;
    };

    return {
      text,
      show,
      onConfirm,
    };
  },
};

Select Date Range

You can select a date range after setting type torange. In range mode, the date returned by the confirm event is an array, the first item in the array is the start time and the second item is the end time.

<van-cell title="Select Date Range" :value="date" @click="show = true" />
<van-calendar v-model:show="show" type="range" @confirm="onConfirm" />
import { ref } from 'vue';

export default {
  setup() {
    const date = ref('');
    const show = ref(false);

    const formatDate = (date) => \`\${date.getMonth() + 1}/\${date.getDate()}\`;
    const onConfirm = (values) => {
      const [start, end] = values;
      show.value = false;
      date.value = \`\${formatDate(start)} - \${formatDate(end)}\`;
    };

    return {
      date,
      show,
      onConfirm,
    };
  },
};

Quick Select

Set show-confirm to false to hide the confirm button. In this case, the confirm event will be emitted immediately after the selection is completed.

<van-calendar v-model:show="show" :show-confirm="false" />

Custom Color

Use color prop to custom calendar color.

<van-calendar v-model:show="show" color="#1989fa" />

Custom Date Range

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

<van-calendar v-model:show="show" :min-date="minDate" :max-date="maxDate" />
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);

    return {
      show,
      minDate: new Date(2010, 0, 1),
      maxDate: new Date(2010, 0, 31),
    };
  },
};

Custom Confirm Text

Use confirm-text and confirm-disabled-text to custom confirm text.

<van-calendar
  v-model:show="show"
  type="range"
  confirm-text="OK"
  confirm-disabled-text="Select End Time"
/>

Custom Day Text

Use formatter to custom day text.

<van-calendar v-model:show="show" type="range" :formatter="formatter" />
export default {
  setup() {
    const formatter = (day) => {
      const month = day.date.getMonth() + 1;
      const date = day.date.getDate();

      if (month === 5) {
        if (date === 1) {
          day.topInfo = 'Labor Day';
        } else if (date === 4) {
          day.topInfo = 'Youth Day';
        } else if (date === 11) {
          day.text = 'Today';
        }
      }

      if (day.type === 'start') {
        day.bottomInfo = 'In';
      } else if (day.type === 'end') {
        day.bottomInfo = 'Out';
      }

      return day;
    };

    return {
      formatter,
    };
  },
};

Custom Position

Use position to custom popup position, can be set to top\u3001left\u3001right.

<van-calendar v-model:show="show" :round="false" position="right" />

Max Range

When selecting a date range, you can use the max-range prop to specify the maximum number of selectable days.

<van-calendar type="range" :max-range="3" :style="{ height: '500px' }" />

Custom First Day Of Week

Use first-day-of-week to custom the start day of week

<van-calendar first-day-of-week="1" />

Tiled display

Set poppable to false, the calendar will be displayed directly on the page instead of appearing as a popup

<van-calendar
  title="Calendar"
  :poppable="false"
  :show-confirm="false"
  :style="{ height: '500px' }"
/>

API

Props

AttributeDescriptionTypeDefault
typeType, can be set to range multiplestringsingle
titleTitle of calendarstringCalendar
colorColor for the bottom button and selected datestring#ee0a24
min-dateMin dateDateToday
max-dateMax dateDateSix months after the today
default-dateDefault selected dateDate | Date[] | nullToday
row-heightRow heightnumber | string64
formatterDay formatter(day: Day) => Day-
poppableWhether to show the calendar inside a popupbooleantrue
lazy-renderWhether to enable lazy renderbooleantrue
show-markWhether to show background month markbooleantrue
show-titleWhether to show titlebooleantrue
show-subtitleWhether to show subtitlebooleantrue
show-confirmWhether to show confirm buttonbooleantrue
readonlyWhether to be readonlybooleanfalse
confirm-textConfirm button textstringConfirm
confirm-disabled-textConfirm button text when disabledstringConfirm
first-day-of-weekSet the start day of week0-60

Calendar Poppable Props

Following props are supported when the poppable is true

AttributeDescriptionTypeDefault
v-model:showWhether to show calendarbooleanfalse
positionPopup position, can be set to top right leftstringbottom
roundWhether to show round cornerbooleantrue
close-on-popstateWhether to close when popstatebooleantrue
close-on-click-overlayWhether to close when overlay is clickedbooleantrue
safe-area-inset-topWhether to enable top safe area adaptationbooleanfalse
safe-area-inset-bottomWhether to enable bottom safe area adaptationbooleantrue
teleportSpecifies a target element where Calendar will be mountedstring | Element-

Calendar Range Props

Following props are supported when the type is range

AttributeDescriptionTypeDefault
max-rangeNumber of selectable daysnumber | stringUnlimited
range-promptError message when exceeded max rangestringChoose no more than xx days
show-range-promptWhether prompt error message when exceeded max rangebooleantrue
allow-same-dayWhether the start and end time of the range is allowed on the same daybooleanfalse

Calendar Multiple Props

Following props are supported when the type is multiple

AttributeDescriptionTypeDefault
max-rangeMax count of selectable daysnumber | stringUnlimited
range-promptError message when exceeded max countstringChoose no more than xx days

Data Structure of Day

KeyDescriptionType
dateDateDate
typeType, can be set to selected\u3001start\u3001middle\u3001end\u3001disabledstring
textTextstring
topInfoTop infostring
bottomInfoBottom infostring
classNameExtra classNamestring

Events

EventDescriptionArguments
selectEmitted when date is selectedvalue: Date | Date[]
confirmEmitted after date selection is complete, if show-confirm is true, it is Emitted after clicking the confirm buttonvalue: Date | Date[]
openEmitted when opening Popup-
closeEmitted when closing Popup-
openedEmitted when Popup is opened-
closedEmitted when Popup is closed-
unselectEmitted when unselect date when type is multiplevalue: Date
month-showEmitted when a month enters the visible areavalue: { date: Date, title: string }
over-rangeEmitted when exceeded max range-
click-subtitle v3.1.3Emitted when clicking the subtitleevent: MouseEvent

Slots

NameDescriptionSlotProps
titleCustom title-
subtitle v3.1.3Custom subtitle-
footerCustom footer-
confirm-text v3.2.6Custom confirm text{ disabled: boolean }
top-info v3.0.17Custom top info of dayday: Day
bottom-info v3.0.17Custom bottom info of dayday: Day

Methods

Use ref to get Calendar instance and call instance methods.

NameDescriptionAttributeReturn value
resetReset selected date, will reset to default date when no params passeddate?: Date | Date[]-
scrollToDateScroll to datedate: Date-
getSelectedDateget selected date-Date | Date[] | null

Types

The component exports the following type definitions:

import type {
  CalendarType,
  CalendarProps,
  CalendarDayItem,
  CalendarDayType,
  CalendarInstance,
} from 'vant';

CalendarInstance is the type of component instance:

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

const calendarRef = ref<CalendarInstance>();

calendarRef.value?.reset();

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-calendar-background-colorvar(--van-background-color-light)-
--van-calendar-popup-height80%-
--van-calendar-header-box-shadow0 2px 10px rgba(125, 126, 128, 0.16)-
--van-calendar-header-title-height44px-
--van-calendar-header-title-font-sizevar(--van-font-size-lg)-
--van-calendar-header-subtitle-font-sizevar(--van-font-size-md)-
--van-calendar-weekdays-height30px-
--van-calendar-weekdays-font-sizevar(--van-font-size-sm)-
--van-calendar-month-title-font-sizevar(--van-font-size-md)-
--van-calendar-month-mark-colorfade(var(--van-gray-2), 80%)-
--van-calendar-month-mark-font-size160px-
--van-calendar-day-height64px-
--van-calendar-day-font-sizevar(--van-font-size-lg)-
--van-calendar-day-margin-bottom4px-
--van-calendar-range-edge-colorvar(--van-white)-
--van-calendar-range-edge-background-colorvar(--van-danger-color)-
--van-calendar-range-middle-colorvar(--van-danger-color)-
--van-calendar-range-middle-background-opacity0.1-
--van-calendar-selected-day-size54px-
--van-calendar-selected-day-colorvar(--van-white)-
--van-calendar-info-font-sizevar(--van-font-size-xs)-
--van-calendar-info-line-heightvar(--van-line-height-xs)-
--van-calendar-selected-day-background-colorvar(--van-danger-color)-
--van-calendar-day-disabled-colorvar(--van-text-color-3)-
--van-calendar-confirm-button-height36px-
--van-calendar-confirm-button-margin7px 0-
`,28),l=[d],i={__name:"README",setup(o,{expose:t}){return t({frontmatter:{}}),(p,c)=>(s(),a("div",n,l))}};export{i as default};