Declaration
Initializers
8 SwiftUI initializers back <DatePicker>. The props you pass pick one.
<DatePicker
selection: Binding<Date>
displayedComponents?: SwiftUI.DatePickerComponents
>
children?: ReactNode
</DatePicker>nonisolated init(
selection: Binding<Date>,
displayedComponents: DatePicker<Label>.Components = [.hourAndMinute, .date],
@ContentBuilder label: () -> Label
)selection*Binding<Date>The date value being displayed and selected.
displayedComponentsSwiftUI.DatePickerComponentsThe date components that user is able to view and edit. Defaults to
[.hourAndMinute, .date]. On watchOS, if.hourAndMinuteor.hourMinuteAndSecondare included with.date, only.dateis displayed.Default
`[.hourAndMinute, .date]`childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
label: () -> Label
Also takes the common view props: modifiers, style and testID.
Overview
Use a DatePicker when you want to provide a view that allows the user to
select a calendar date, and optionally a time. The view binds to a
doc://com.apple.documentation/documentation/Foundation/Date instance.
The following example creates a basic DatePicker, which appears on iOS as
text representing the date. This example limits the display to only the
calendar date, not the time. When the user taps or clicks the text, a
calendar view animates in, from which the user can select a date. When the
user dismisses the calendar view, the view updates the bound
doc://com.apple.documentation/documentation/Foundation/Date.
@State private var date = Date()
var body: some View {
DatePicker(
"Start Date",
selection: $date,
displayedComponents: [.date]
)
}For cases where adding a subtitle to the label is desired, use a view
builder that creates multiple Text views where the first text represents
the title and the second text represents the subtitle:
@State private var date = Date()
var body: some View {
DatePicker(selection: $date) {
Text("Start Date")
Text("Select the starting date for the event")
}
}You can limit the DatePicker to specific ranges of dates, allowing
selections only before or after a certain date, or between two dates. The
following example shows a date-and-time picker that only permits selections
within the year 2021 (in the UTC time zone).
@State private var date = Date()
let dateRange: ClosedRange<Date> = {
let calendar = Calendar.current
let startComponents = DateComponents(year: 2021, month: 1, day: 1)
let endComponents = DateComponents(year: 2021, month: 12, day: 31, hour: 23, minute: 59, second: 59)
return calendar.date(from:startComponents)!
...
calendar.date(from:endComponents)!
}()
var body: some View {
DatePicker(
"Start Date",
selection: $date,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
}Styling date pickers
To use a different style of date picker, use the
View.datePickerStyle(_:) view modifier. The following example shows the
graphical date picker style.
@State private var date = Date()
var body: some View {
DatePicker(
"Start Date",
selection: $date,
displayedComponents: [.date]
)
.datePickerStyle(.graphical)
}From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Controls.
- ButtonA control that initiates an action.
- ColorPickerA control used to select a color from the system color picker UI.
- DisclosureGroupA view that shows or hides another content view, based on the state of a disclosure control.
- MultiDatePickerA control for picking multiple dates.
- OutlineGroupA structure that computes views and disclosure groups on demand from an underlying collection of tree-structured, identified data.
- PasteButtonA system button that reads items from the pasteboard and delivers it to a closure.
- PickerA control for selecting from a set of mutually exclusive values.
- SecureFieldA control into which people securely enter private text.
- SliderA control for selecting a value from a bounded linear range of values.
- StepperA control that performs increment and decrement actions.
- SubscriptionViewA view that subscribes to a publisher with an action.
- TableA container that presents rows of data arranged in one or more columns, optionally providing the ability to select one or more members.