For DatePicker View, we have the option to show only date or time components as well and it can be done with the help of displayedComponents a parameter which takes a collection of DatePickerComponents. We can choose to show .date, .hourAndMinutes at the same time or one of them.
struct DatePickerExample: View {
@State private var date = Date()
var body: some View {
DatePicker("Pick a date", selection: $date, displayedComponents: [.date])
.padding()
}
}
We can change our example to display time view for DatePicker by simply replacing .date with .hourAndMinute
struct DatePickerExample: View {
@State private var date = Date()
var body: some View {
DatePicker("Pick a time", selection: $date, displayedComponents: [.hourAndMinute])
.padding()
}
}