DatePicker()
Jan 18, 2024
The DatePicker is a native SwiftUI component for selecting date and/or time.
You can implement it using various initializers and customize it with different DatePickerStyles.
Additionally, a DateFormatter can be used to effectively display dates in your visionOS application.
1. Choose DatePicker
- Select the on with title & selection parenthesis (most general)
- Bind the today’s date variable
2. Stylize the component
.tint()isn’t working in DatePicker
.datePickerStyle()has three styles: compact, graphical, wheel
3. Select displayed component
- Select the one with title, selection, displayComponents
- We can choose which components will show with
[.date, .hourAndMinute] - Can delete
.dateto only show.hourAndMinuteand vice versa
4. Add Closed Range
- Select the one with
in: ColsedRange<Date>.
- Create a Date let with starting from year 2018 to Today
4.1 Edit the display component
- Add
displayedComponents: [.date, .hourAndMinute]to edit the diplay
5. Display the Date() into string
- We can bring the Date() data into string
- We have to format the date by creating a function
- It creates variable called dateFormatter with
DateFormatter{}component
- which means, it has to return
DateFormatter()by calling formatter
- Now, formatter is DateFormatter()
- formatter.dateStyle = .short → DateFormatter().dateStyle = .short
- return formatter
- We change the description
to