Set minimumDate to the start of today and maximumDate to Calendar.current.date(byAdding: .month, value: 12, to: today). The picker then won't let anyone pick a past date or one more than a year out. Choose a style with preferredDatePickerStyle (.wheels fits a text field's input view, .inline shows a calendar), and still check the date again before you send it anywhere.
SWIFT TIPS · UIKIT
First posted on July 29, 2018 for Swift 4, as "Disble Past Date in UIDatePicker MinimumDate and MaximumDate". Rewritten on November 17, 2026. The UIKit code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target, but not run on a device. The date calculations were run on a Mac with Swift 6.2.3.
In 2018 I needed a date field that only accepted today and future dates, up to a year ahead. The post used NSCalendar, NSDate and NSDateComponents, added zero days for the minimum and 12 months for the maximum, and set them on a UIDatePicker that opened as the text field's input view, with an Indonesian locale and a Done button.
The idea is right. The Objective-C types can go, and two details deserve more care: what "today" means, and what happens at the end of a month.
The picker, today
import UIKit
final class BookingViewController: UIViewController {
@IBOutlet private weak var dateField: UITextField!
private let picker = UIDatePicker()
private let displayFormat = Date.FormatStyle(date: .long, time: .omitted)
override func viewDidLoad() {
super.viewDidLoad()
// Today (from midnight) up to 12 months ahead, in the user's calendar
let calendar = Calendar.current
let today = calendar.startOfDay(for: Date())
picker.datePickerMode = .date
picker.minimumDate = today
picker.maximumDate = calendar.date(byAdding: .month, value: 12, to: today)
// Wheels fit inside a keyboard-sized input view. .inline shows a calendar instead.
picker.preferredDatePickerStyle = .wheels
picker.addAction(UIAction { [weak self] _ in self?.showSelectedDate() }, for: .valueChanged)
let toolbar = UIToolbar()
toolbar.sizeToFit()
toolbar.items = [
UIBarButtonItem(systemItem: .flexibleSpace),
UIBarButtonItem(systemItem: .done, primaryAction: UIAction { [weak self] _ in
self?.view.endEditing(true)
}),
]
dateField.inputView = picker
dateField.inputAccessoryView = toolbar
}
private func showSelectedDate() {
dateField.text = picker.date.formatted(displayFormat)
}
}
// A picker shown on the screen itself, without a text field
final class InlineBookingViewController: UIViewController {
private let picker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
picker.datePickerMode = .date
picker.preferredDatePickerStyle = .inline
picker.minimumDate = Calendar.current.startOfDay(for: Date())
picker.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(picker)
NSLayoutConstraint.activate([
picker.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor),
picker.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
picker.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
])
}
}
Today means midnight, not now
The 2018 code used the current moment as the minimum. If someone opens the picker at 15:42, today's date is still allowed, but in time mode, or if you compare dates later, "now" excludes the earlier part of today. Calendar.current.startOfDay(for: Date()) gives midnight in the user's time zone, which is what "no past dates" usually means.
Apple's documentation adds a rule worth knowing: if minimumDate is later than maximumDate, the picker ignores both. A bug that produces a maximum in the past doesn't show an error. It just lets every date through.
Adding months at the end of a month
I ran the date math on a Mac with a Gregorian calendar in Jakarta time:
now: 2026-11-17T15:42:00+0700
now + 12 months: 2027-11-17T15:42:00+0700
startOfDay(now): 2026-11-17T00:00:00+0700
Jan 31 + 1 month: 2027-02-28T10:00:00+0700
Feb 29, 2028 + 12 months: 2029-02-28T10:00:00+0700
2018 way, day = 0: 2026-11-17T15:42:00+0700
Two lines matter. January 31 plus one month is February 28, not March 3. February 29, 2028 plus 12 months is February 28, 2029. Calendar clamps to the last valid day instead of overflowing into the next month, which is what you want for a booking window. The last line shows that the 2018 trick of adding a component with day = 0 returns the same moment, so you can use the date directly.
Pick a style on purpose
Since iOS 13.4, UIDatePicker has preferredDatePickerStyle, and Apple's documentation says the default is .automatic, which lets the system choose. That may not be the spinning wheel the 2018 code was written for, and a compact or inline picker doesn't fit well as a text field's inputView. The code sets .wheels for the input-view case. The second class shows the alternative: an .inline calendar placed on the screen itself, without a text field.
Show the date in the user's language
The 2018 version forced Locale(identifier: "id") and then formatted the result with a fixed "dd MMMM yyyy" pattern. That's fine for an app that only ships in Indonesian. For everyone else, leave the picker's locale alone and format with Date.FormatStyle(date: .long, time: .omitted), which follows each user's language and region. The valueChanged action updates the field as the wheel turns, so the Done button only has to close the keyboard.
The picker isn't validation
The limits only apply to what the picker shows. A date can still reach your server from an old app version, a pasted value, or a device whose clock is wrong. Check the range again on the server, and on the device right before you submit if the date matters.
A quick check for your project
- Search for
minimumDateandmaximumDate. Check that the minimum usesstartOfDaywhen it means "today". - Search for
NSCalendarandNSDateComponentsin Swift files.CalendarandDateComponentsdo the same without the casts. - Open each date picker on iOS 15 and on the newest iOS you support, and check which style you get.
What the 2018 version said
Besides the min and max dates, the full code built a toolbar with a Done button and formatted the result twice. It referred to the text field as TextTanggal in a few places, but the outlet was declared as textDate. Type-checked as posted, it stops in both Swift 5 and Swift 6 mode with "cannot find 'TextTanggal' in scope". Renaming one of them fixes it.
Keep reading on JR Devhub
Comments