Disable future date selection for DatePicker in SwiftUI

Introduction

We can limit the DatePicker to specific ranges of dates, allowing date selections only before or, after a certain date, or between two dates.

Limiting the date selection

We can choose to limit date selection to current date meaning no future date selection is allowed. We can achieve this by setting the DatePicker’s in parameter:

1
2
3
4
5
6
DatePicker(
  "",
  selection: $selection,
  in: ...Date(),
  displayedComponents: [.date]
)

img1

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy