Back to Shadcn Ui

Date Picker

apps/v4/content/docs/components/radix/date-picker.mdx

latest2.6 KB
Original Source
<ComponentPreview styleName="radix-nova" name="date-picker-demo" />

Installation

The Date Picker is built using a composition of the <Popover /> and the <Calendar /> components.

See installation instructions for the Popover and the Calendar components.

Usage

tsx
"use client"

import * as React from "react"
import { format } from "date-fns"
import { Calendar as CalendarIcon } from "lucide-react"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { Calendar } from "@/components/ui/calendar"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"

export function DatePickerDemo() {
  const [date, setDate] = React.useState<Date>()

  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button
          variant="outline"
          data-empty={!date}
          className="w-[280px] justify-start text-left font-normal data-[empty=true]:text-muted-foreground"
        >
          <CalendarIcon />
          {date ? format(date, "PPP") : <span>Pick a date</span>}
        </Button>
      </PopoverTrigger>
      <PopoverContent className="w-auto p-0">
        <Calendar mode="single" selected={date} onSelect={setDate} />
      </PopoverContent>
    </Popover>
  )
}

See the React DayPicker documentation for more information.

Examples

Basic

A basic date picker component.

<ComponentPreview styleName="radix-nova" name="date-picker-basic" />

Range Picker

A date picker component for selecting a range of dates.

<ComponentPreview styleName="radix-nova" name="date-picker-range" />

Date of Birth

A date picker component for selecting a date of birth. This component includes a dropdown caption layout for date and month selection.

<ComponentPreview styleName="radix-nova" name="date-picker-dob" />

Input

A date picker component with an input field for selecting a date.

<ComponentPreview styleName="radix-nova" name="date-picker-input" />

Time Picker

A date picker component with a time input field for selecting a time.

<ComponentPreview styleName="radix-nova" name="date-picker-time" />

Natural Language Picker

This component uses the chrono-node library to parse natural language dates.

<ComponentPreview styleName="radix-nova" name="date-picker-natural-language" />

RTL

To enable RTL support in shadcn/ui, see the RTL configuration guide.

<ComponentPreview styleName="radix-nova" name="date-picker-rtl" direction="rtl" />