You can use the .maximumDate()
modifier of the DatePicker
to set the maximum selectable date to the current date and time. This will prevent the user from selecting a date or time in the future.
Here's an example:
struct ContentView: View {
@State private var selectedDate = Date()
var body: some View {
VStack {
Text("Select a date and time:")
DatePicker(
"",
selection: $selectedDate,
displayedComponents: [.date, .hourAndMinute]
)
.labelsHidden()
.maximumDate(Date())
}
}
}
In the example above, we're using the .maximumDate(Date())
modifier to set the maximum selectable date to the current date and time. You can customize the format of the displayed date and time components using the displayedComponents
parameter.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-10-09 11:00:00 +0000
Seen: 17 times
Last updated: Feb 16 '23
How to add a custom button in SwiftUI for iOS13 that enables going back between screens?
How can the update of properties from both ParentViewModel and ChildViewModel in SwiftUI be solved?
Can SwiftUI be utilized to restrict the touch region for a drag gesture?
How can the color of a selected element (Picker) be modified in SwiftUI for WatchOS?
How to make a calendar layout that is horizontal using Swiftui?
What is the process to add a radio button with a check mark on a TableView cell using Swift?
How to limit UITextField input to only accept numerical values?