🌐 Nodejs.cn

日期选择器

带有范围和预设的日期选择器组件。

"use client"

import * as React from "react"

安装

🌐 Installation

日期选择器是由 <Popover /><Calendar /> 组件组合构建的。

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

请参阅 PopoverCalendar 组件的安装说明。

🌐 See installation instructions for the Popover and the Calendar components.

用法

🌐 Usage

components/example-date-picker.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>
  )
}

有关更多信息,请参阅 React DayPicker 文档。

🌐 See the React DayPicker documentation for more information.

示例

🌐 Examples

基础

🌐 Basic

一个基本的日期选择器组件。

🌐 A basic date picker component.

"use client"

import * as React from "react"

范围选择器

🌐 Range Picker

用于选择日期范围的日期选择器组件。

🌐 A date picker component for selecting a range of dates.

"use client"

import * as React from "react"

出生日期

🌐 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.

"use client"

import * as React from "react"

输入

🌐 Input

一个带有输入字段的日期选择组件,用于选择日期。

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

"use client"

import * as React from "react"

时间选择器

🌐 Time Picker

一个带有时间输入字段的日期选择器组件,用于选择时间。

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

"use client"

import * as React from "react"

自然语言选择器

🌐 Natural Language Picker

该组件使用 chrono-node 库来解析自然语言日期。

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

Your post will be published on March 09, 2026.
"use client"

import * as React from "react"

从右到左

🌐 RTL

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

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

"use client"

import * as React from "react"