--- title: Calendar description: A calendar component that allows users to select a date or a range of dates. base: base component: true links: doc: https://react-day-picker.js.org --- ## Installation Command Manual ```bash npx shadcn@latest add calendar ``` Install the following dependencies: ```bash npm install react-day-picker date-fns ``` Add the `Button` component to your project. The `Calendar` component uses the `Button` component. Make sure you have it installed in your project. Copy and paste the following code into your project. Update the import paths to match your project setup. ## Usage ```tsx showLineNumbers import { Calendar } from "@/components/ui/calendar" ``` ```tsx showLineNumbers const [date, setDate] = React.useState(new Date()) return ( ) ``` See the [React DayPicker](https://react-day-picker.js.org) documentation for more information. ## About The `Calendar` component is built on top of [React DayPicker](https://react-day-picker.js.org). ## Date Picker You can use the `` component to build a date picker. See the [Date Picker](/docs/components/base/date-picker) page for more information. ## Persian / Hijri / Jalali Calendar To use the Persian calendar, edit `components/ui/calendar.tsx` and replace `react-day-picker` with `react-day-picker/persian`. ```diff - import { DayPicker } from "react-day-picker" + import { DayPicker } from "react-day-picker/persian" ``` ## Selected Date (With TimeZone) The Calendar component accepts a `timeZone` prop to ensure dates are displayed and selected in the user's local timezone. ```tsx showLineNumbers export function CalendarWithTimezone() { const [date, setDate] = React.useState(undefined) const [timeZone, setTimeZone] = React.useState(undefined) React.useEffect(() => { setTimeZone(Intl.DateTimeFormat().resolvedOptions().timeZone) }, []) return ( ) } ``` **Note:** If you notice a selected date offset (for example, selecting the 20th highlights the 19th), make sure the `timeZone` prop is set to the user's local timezone. **Why client-side?** The timezone is detected using `Intl.DateTimeFormat().resolvedOptions().timeZone` inside a `useEffect` to ensure compatibility with server-side rendering. Detecting the timezone during render would cause hydration mismatches, as the server and client may be in different timezones. ## Examples ### Basic A basic calendar component. We used `className="rounded-lg border"` to style the calendar. ### Range Calendar Use the `mode="range"` prop to enable range selection. ### Month and Year Selector Use `captionLayout="dropdown"` to show month and year dropdowns. ### Presets ### Date and Time Picker ### Booked dates ### Custom Cell Size You can customize the size of calendar cells using the `--cell-size` CSS variable. You can also make it responsive by using breakpoint-specific values: ```tsx showLineNumbers ``` Or use fixed values: ```tsx showLineNumbers ``` ### Week Numbers Use `showWeekNumber` to show week numbers. ## RTL To enable RTL support in shadcn/ui, see the [RTL configuration guide](/docs/rtl). See also the [Hijri Guide](#persian--hijri--jalali-calendar) for enabling the Persian / Hijri / Jalali calendar. When using RTL, import the locale from `react-day-picker/locale` and pass both the `locale` and `dir` props to the Calendar component: ```tsx showLineNumbers import { arSA } from "react-day-picker/locale" ; ``` ## API Reference See the [React DayPicker](https://react-day-picker.js.org) documentation for more information on the `Calendar` component. ## Changelog ### RTL Support If you're upgrading from a previous version of the `Calendar` component, you'll need to apply the following updates to add locale support: Import the `Locale` type. Add `Locale` to your imports from `react-day-picker`: ```diff 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: ```diff function Calendar({ className, classNames, showOutsideDays = true, captionLayout = "label", buttonVariant = "ghost", + locale, formatters, components, ...props }: React.ComponentProps & { buttonVariant?: React.ComponentProps["variant"] }) { ``` Pass `locale` to DayPicker. Pass the `locale` prop to the `DayPicker` component: ```diff - 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`: ```diff function CalendarDayButton({ className, day, modifiers, + locale, ...props - }: React.ComponentProps) { + }: React.ComponentProps & { locale?: Partial }) { ``` Update date formatting in CalendarDayButton. Use `locale?.code` in the date formatting: ```diff