Skip to content

Calendar 日历

介绍

用于日历展示、选择日期或日期区间。

基础用法

通过 v-model 绑定选中的日期/年份,通过 type 设置日历的选择类型,支持设置 date month year 类型的日历。

html
<!-- 日期选择 -->
<vd-calendar v-model="date" />
<!-- 月份选择 -->
<vd-calendar v-model="month" type="month" />
<!-- 年份选择 -->
<vd-calendar v-model="year" type="year" />
ts
const date = ref('2025-01-01');
const month = ref('2025-01');
const year = ref('2025');

展示重置按钮

通过 resetable 属性展示重置按钮,设置后会改变日历顶部操作布局,点击重置按钮后会回到当前选中值,未选中时回到 今天

html
<vd-calendar v-model="date" resetable />
ts
const date = ref('2025-01-01');

设置选择类型

通过 select-type 属性设置选择类型,支持 single multiple range week 类型选择方式。

html
<!-- 单选 -->
<vd-calendar v-model="single" />
<!-- 多选 -->
<vd-calendar v-model="multiple" select-type="multiple" />
<!-- 范围选择 -->
<vd-calendar v-model="range" select-type="range" />
<!-- 星期选择 -->
<vd-calendar v-model="week" select-type="week" />
ts
const single = ref('2025-01-01');
const multiple = ref([
  '2025-01-01',
  '2025-02-01',
  '2025-03-01',
]);
const range = ref(['2025-05-01', '2025-06-01']);
const week = ref(['2025-05-11', '2025-05-17']);

自定义周起始日

通过 week-first 属性设置周起始日,通过 0~6 数值来设置(周日为 0,周一至周六为 1-6),设置后会影响 week 选择类型的起始日期。

html
<vd-calendar
  v-model="week"
  select-type="week"
  week-first="1"
/>
ts
const week = ref([]);

自定义描述类型

通过 desc-type 属性设置描述类型,支持 festival term lunar 三种类型,可设置多个,按数组排序来决定显示的优先级。

html
<vd-calendar
  v-model="date"
  :desc-type="descType"
/>
ts
import { CalendarDateDescType } from 'vital-design';

const descType: CalendarDateDescType[] = [
  'festival', // 节日
  'term', // 节气
  'lunar', // 农历
];

const date = ref('');

日期限制

通过 minmax 属性设置最大、最小日期限制,支持 dayjs 日期。

html
<vd-calendar
  v-model="date"
  :min="limit.min"
  :max="limit.max"
/>
ts
import dayjs from 'dayjs';

const date = ref('');

// 字符日期
const limit = reactive({
  min: '2025-01-01',
  max: '2026-01-01',
});

// dayjs日期
const limit2 = reactive({
  min: dayjs().subtract(1, 'month'),
  max: dayjs().add(1 ,'month'),
});

单独使用视图

可根据需求单独使用日期、月份、年份选择器。

html
<!-- 日期视图 -->
<vd-calendar-date v-model="date" :show-mark="true" />
<!-- 月份视图 -->
<vd-calendar-month v-model="month" :show-mark="true" />
<!-- 年份视图 -->
<vd-calendar-year v-model="year" />
ts
const date = ref('2025-01-01');
const month = ref('2025-01');
const year = ref('2025');

API

Calendar Props

参数说明类型默认值
v-model选中日期string | number | Date | (string | number | Date)[]-
active激活日期string | number | Date今天
type日历类型CalendarTypedate
select-type选择类型CalendarSelectTypesingle
min最小日期string | number | Date | dayjs.Dayjs十年前
max最大日期string | number | Date | dayjs.Dayjs十年后
week-first周起始日number以 dayjs 为准
desc-type描述类型CalendarDateDescType[][]
show-mark是否显示背景水印booleanfalse
allow-same区间选择时,是否允许开始日期等于结束日期booleantrue
resetable是否展示重置按钮booleanfalse
switchable是否允许点击标题切换日历类型booleantrue
swipeable是否开启左右滑动手势切换booleantrue
cell-class格子类名格式化方法CalendarCellClass-
cell-text格子文字格式化方法CalendarCellText-
cell-desc格子描述格式化方法CalendarCellText-

CalendarDate Props

参数说明类型默认值
v-model选中日期string | number | Date | (string | number | Date)[]-
active激活月份string | number | Date今天
select-type选择类型CalendarSelectTypesingle
min-date最小日期string | number | Date | dayjs.Dayjs十年前
max-date最大日期string | number | Date | dayjs.Dayjs十年后
week-first周起始日number以 dayjs 为准
desc-type描述类型CalendarDateDescType[][]
show-mark是否显示背景水印booleanfalse
allow-same区间选择时,是否允许开始日期等于结束日期booleantrue
cell-class格子类名格式化方法CalendarCellClass-
cell-text格子文字格式化方法CalendarCellText-
cell-desc格子描述格式化方法CalendarCellText-

CalendarMonth Props

参数说明类型默认值
v-model选中月份string | number | Date | (string | number | Date)[]-
active激活年份string | number今年
select-type选择类型CalendarSelectTypesingle
min-month最小月份日期string | number | Date | dayjs.Dayjs十年前
max-month最大月份日期string | number | Date | dayjs.Dayjs十年后
show-mark是否显示背景水印booleanfalse
allow-same区间选择时,是否允许开始日期等于结束日期booleantrue
cell-class格子类名格式化方法CalendarCellClass-
cell-text格子文字格式化方法CalendarCellText-
cell-desc格子描述格式化方法CalendarCellText-

CalendarYear Props

参数说明类型默认值
v-model选中月份string | number | (string | number)[]-
active激活年份string | number今年
select-type选择类型CalendarSelectTypesingle
min-year最小年份string | number十年前
max-year最大年份string | number十年后
allow-same区间选择时,是否允许开始日期等于结束日期booleantrue
cell-class格子类名格式化方法CalendarCellClass-
cell-text格子文字格式化方法CalendarCellText-
cell-desc格子描述格式化方法CalendarCellText-

代替原始 Props

参数说明类型默认值
custom-id根节点idstring-
custom-class根节点类名ClassValue-
custom-style根节点样式StyleValue-

Events

事件名说明回调参数
click点击时触发MouseEvent
click-date点击日期时触发CalendarDateItem
click-month点击月份时触发CalendarMonthItem
click-year点击年份时触发CalendarYearItem

Slots

名称说明
title标题
prev上一个按钮
next下一个按钮
reset重置按钮

赣ICP备2025061025号-1