111k

Calendar

一个允许用户选择单个日期或日期范围的日历组件。

March 2026
"use client"

import * as React from "react"

安装

pnpm dlx shadcn@latest add calendar

用法

import { Calendar } from "@/components/ui/calendar"
const [date, setDate] = React.useState<Date | undefined>(new Date())
 
return (
  <Calendar
    mode="single"
    selected={date}
    onSelect={setDate}
    className="rounded-lg border"
  />
)

更多信息请参见 React DayPicker 文档。

关于

Calendar 组件基于 React DayPicker 构建。

日期选择器

你可以使用 <Calendar> 组件来构建日期选择器。更多信息请参见 Date Picker 页面。

波斯 / 伊斯兰 / Jalali 日历

要使用波斯日历,请编辑 components/ui/calendar.tsx,并将 react-day-picker 替换为 react-day-picker/persian

- import { DayPicker } from "react-day-picker"
+ import { DayPicker } from "react-day-picker/persian"
خرداد ۱۴۰۴
"use client"

import * as React from "react"

带时区的选定日期

Calendar 组件接受一个 timeZone 属性,以确保日期以用户的本地时区显示和选择。

export function CalendarWithTimezone() {
  const [date, setDate] = React.useState<Date | undefined>(undefined)
  const [timeZone, setTimeZone] = React.useState<string | undefined>(undefined)
 
  React.useEffect(() => {
    setTimeZone(Intl.DateTimeFormat().resolvedOptions().timeZone)
  }, [])
 
  return (
    <Calendar
      mode="single"
      selected={date}
      onSelect={setDate}
      timeZone={timeZone}
    />
  )
}

注意: 如果你发现选定日期有偏移(例如,选择 20 号却高亮了 19 号),请确保将 timeZone 属性设置为用户的本地时区。

为什么在客户端处理? 时区通过 useEffect 内的 Intl.DateTimeFormat().resolvedOptions().timeZone 检测,以确保与服务端渲染兼容。在渲染期间检测时区会导致 hydration 不匹配,因为服务器和客户端可能处于不同的时区。

示例

基础

一个基础日历组件。我们使用 className="rounded-lg border" 来设置日历样式。

March 2026
"use client"

import { Calendar } from "@/components/ui/calendar"

范围选择日历

使用 mode="range" 属性启用范围选择。

January 2026
February 2026
"use client"

import * as React from "react"

月份和年份选择器

使用 captionLayout="dropdown" 显示月份和年份下拉框。

March 2026
"use client"

import { Calendar } from "@/components/ui/calendar"

预设

March 2026
"use client"

import * as React from "react"

日期和时间选择器

March 2026
"use client"

import * as React from "react"

已预订日期

February 2026
"use client"

import * as React from "react"

自定义单元格大小

December 2026
"use client"

import * as React from "react"

你可以使用 --cell-size CSS 变量来自定义日历单元格大小。也可以使用特定断点值让它响应式变化:

<Calendar
  mode="single"
  selected={date}
  onSelect={setDate}
  className="rounded-lg border [--cell-size:--spacing(11)] md:[--cell-size:--spacing(12)]"
/>

或者使用固定值:

<Calendar
  mode="single"
  selected={date}
  onSelect={setDate}
  className="rounded-lg border [--cell-size:2.75rem] md:[--cell-size:3rem]"
/>

周数

使用 showWeekNumber 来显示周数。

February 2026
06
07
08
09
"use client"

import * as React from "react"

RTL

要在 shadcn/ui 中启用 RTL 支持,请参见 RTL 配置指南

另请参见 Hijri 指南,了解如何启用波斯 / 伊斯兰 / Jalali 日历。

مارس 2026
"use client"

import * as React from "react"

在使用 RTL 时,请从 react-day-picker/locale 导入 locale,并将 localedir 两个属性传给 Calendar 组件:

import { arSA } from "react-day-picker/locale"
 
;<Calendar
  mode="single"
  selected={date}
  onSelect={setDate}
  locale={arSA}
  dir="rtl"
/>

API 参考

更多关于 Calendar 组件的信息,请参见 React DayPicker 文档。

更新日志

RTL Support

如果你正在从旧版本的 Calendar 组件升级,则需要应用以下更新来添加语言环境支持:

导入 Locale 类型。

Add Locale to your imports from react-day-picker:

  import {
    DayPicker,
    getDefaultClassNames,
    type DayButton,
+   type Locale,
  } from "react-day-picker"

Add locale prop to the Calendar component.

Add the locale prop to the component's props:

  function Calendar({
    className,
    classNames,
    showOutsideDays = true,
    captionLayout = "label",
    buttonVariant = "ghost",
+   locale,
    formatters,
    components,
    ...props
  }: React.ComponentProps<typeof DayPicker> & {
    buttonVariant?: React.ComponentProps<typeof Button>["variant"]
  }) {

Pass locale to DayPicker.

Pass the locale prop to the DayPicker component:

    <DayPicker
      showOutsideDays={showOutsideDays}
      className={cn(...)}
      captionLayout={captionLayout}
+     locale={locale}
      formatters={{
        formatMonthDropdown: (date) =>
-         date.toLocaleString("default", { month: "short" }),
+         date.toLocaleString(locale?.code, { month: "short" }),
        ...formatters,
      }}

Update CalendarDayButton to accept locale.

Update the CalendarDayButton component signature and pass locale:

  function CalendarDayButton({
    className,
    day,
    modifiers,
+   locale,
    ...props
- }: React.ComponentProps<typeof DayButton>) {
+ }: React.ComponentProps<typeof DayButton> & { locale?: Partial<Locale> }) {

Update date formatting in CalendarDayButton.

Use locale?.code in the date formatting:

    <Button
      variant="ghost"
      size="icon"
-     data-day={day.date.toLocaleDateString()}
+     data-day={day.date.toLocaleDateString(locale?.code)}
      ...
    />

locale 传递给 DayButton 组件。

更新 DayButton 组件的用法以传递 locale 属性:

      components={{
        ...
-       DayButton: CalendarDayButton,
+       DayButton: ({ ...props }) => (
+         <CalendarDayButton locale={locale} {...props} />
+       ),
        ...
      }}

Update RTL-aware CSS classes.

Replace directional classes with logical properties for better RTL support:

  // In the day classNames:
- [&:last-child[data-selected=true]_button]:rounded-r-(--cell-radius)
+ [&:last-child[data-selected=true]_button]:rounded-e-(--cell-radius)
- [&:nth-child(2)[data-selected=true]_button]:rounded-l-(--cell-radius)
+ [&:nth-child(2)[data-selected=true]_button]:rounded-s-(--cell-radius)
- [&:first-child[data-selected=true]_button]:rounded-l-(--cell-radius)
+ [&:first-child[data-selected=true]_button]:rounded-s-(--cell-radius)
 
  // In range_start classNames:
- rounded-l-(--cell-radius) ... after:right-0
+ rounded-s-(--cell-radius) ... after:end-0
 
  // In range_end classNames:
- rounded-r-(--cell-radius) ... after:left-0
+ rounded-e-(--cell-radius) ... after:start-0
 
  // In CalendarDayButton className:
- data-[range-end=true]:rounded-r-(--cell-radius)
+ data-[range-end=true]:rounded-e-(--cell-radius)
- data-[range-start=true]:rounded-l-(--cell-radius)
+ data-[range-start=true]:rounded-s-(--cell-radius)

应用这些更改后,你可以使用 locale 属性提供特定语言环境的格式化:

import { enUS } from "react-day-picker/locale"
 
;<Calendar mode="single" selected={date} onSelect={setDate} locale={enUS} />