Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

DatePicker

用于选择日期和时间的 SwiftUI DatePicker 组件。

iOS
Included in Expo Go
Recommended version:
~58.0.1

Expo UI DatePicker 与官方 SwiftUI DatePicker API 相匹配,并支持通过 datePickerStyle 修饰符设置样式。

Form 中的日期和时间 DatePicker 行Form 中的日期和时间 DatePicker 行

安装

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

日期选择器

DatePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; export default function DatePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); // A date picker fills the width it is given, so `matchContents` collapses it. return ( <Host style={{ flex: 1 }}> <DatePicker title="Select a date" selection={selectedDate} displayedComponents={['date']} onDateChange={date => { setSelectedDate(date); }} /> </Host> ); }

时间选择器

TimePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; export default function TimePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker title="Select a time" selection={selectedDate} displayedComponents={['hourAndMinute']} onDateChange={date => { setSelectedDate(date); }} /> </Host> ); }

日期和时间选择器

DateTimePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; export default function DateTimePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker title="Select date and time" selection={selectedDate} displayedComponents={['date', 'hourAndMinute']} onDateChange={date => { setSelectedDate(date); }} /> </Host> ); }

使用日期范围

DateRangePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; export default function DateRangePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker title="Select a date" selection={selectedDate} displayedComponents={['date']} range={{ start: new Date(2024, 0, 1), end: new Date(2024, 11, 31), }} onDateChange={date => { setSelectedDate(date); }} /> </Host> ); }

使用修饰符设置样式

你可以使用 datePickerStyle 修饰符更改选择器的外观。可用样式包括:automatic、compact、graphical 和 wheel。

WheelDatePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; import { datePickerStyle } from '@expo/ui/swift-ui/modifiers'; export default function WheelDatePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker modifiers={[datePickerStyle('wheel')]} title="Select a date" selection={selectedDate} displayedComponents={['date']} onDateChange={date => { setSelectedDate(date); }} /> </Host> ); }
GraphicalDatePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; import { datePickerStyle } from '@expo/ui/swift-ui/modifiers'; export default function GraphicalDatePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker modifiers={[datePickerStyle('graphical')]} title="Select a date" selection={selectedDate} displayedComponents={['date']} onDateChange={date => { setSelectedDate(date); }} /> </Host> ); }

禁用选择器

你可以使用 disabled 修饰符使选择器不可交互。

DisabledDatePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; import { disabled } from '@expo/ui/swift-ui/modifiers'; export default function DisabledDatePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker title="Select a date" selection={selectedDate} displayedComponents={['date']} onDateChange={date => { setSelectedDate(date); }} modifiers={[disabled()]} /> </Host> ); }

自定义区域设置

使用 environment 修饰符和 locale 键,以特定区域设置显示选择器。

LocaleDatePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; import { environment } from '@expo/ui/swift-ui/modifiers'; export default function LocaleDatePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker title="Sélectionner la date" selection={selectedDate} displayedComponents={['date']} onDateChange={date => { setSelectedDate(date); }} modifiers={[environment('locale', 'fr_FR')]} /> </Host> ); }

自定义时区

使用 environment 修饰符和 timeZone 键,以特定 IANA 时区显示选择器。

TimeZoneDatePickerExample.tsx
import { useState } from 'react'; import { Host, DatePicker } from '@expo/ui/swift-ui'; import { environment } from '@expo/ui/swift-ui/modifiers'; export default function TimeZoneDatePickerExample() { const [selectedDate, setSelectedDate] = useState(new Date()); return ( <Host style={{ flex: 1 }}> <DatePicker title="Tokyo time" selection={selectedDate} displayedComponents={['date', 'hourAndMinute']} onDateChange={date => { setSelectedDate(date); }} modifiers={[environment('timeZone', 'Asia/Tokyo')]} /> </Host> ); }

API

import { DatePicker } from '@expo/ui/swift-ui';

Component

DatePicker

iOS

Type: React.Element<DatePickerProps>

Renders a SwiftUI DatePicker component.

DatePickerProps

children

iOS
Optional • Type: ReactNode

Children to use as a custom label.

displayedComponents

iOS
Optional • Type: DatePickerComponent[] • Default: ['date']

The components to display: 'date' and/or 'hourAndMinute'.

onDateChange

iOS
Optional • Type: (date: Date) => void

Callback when the date selection changes.

range

iOS
Optional • Type: DateRange

The selectable date range.

selection

iOS
Optional • Type: Date

The currently selected date.

title

iOS
Optional • Type: string

A title/label displayed on the picker.

Types

DatePickerComponent

iOS

Literal type: string

Acceptable values are: 'date' | 'hourAndMinute'

DateRange

iOS
PropertyTypeDescription
end(optional)Date
-
start(optional)Date
-