Search code examples
swiftdatetimeuidatepicker

How to limit date selection on UIDatePicker


I created a UIDatePicker and set the minimum selectable date. However I would like to know how can I limit selection so that user only may be able to select minutes by 5 minute intervals.

And as a result user can only select for example: 12:00, 12:05, 12:10, 12:15, 12:20, 12:25, 12:30 ... 12:55

This is my code so far:

@IBOutlet weak var datePicker: UIDatePicker!

override func viewDidLoad() {
   super.viewDidLoad()
   datePicker.preferredDatePickerStyle = .wheels
   datePicker.minimumDate = Date(timeIntervalSinceNow: 7200) // 7200 is equal to 2 hours
}

Solution

  • Configure your picker with a minuteInterval

    datePicker.minuteInterval = 5
    

    will make the picker increment in 5 minute intervals.

    You can set the minute interval to whatever interval you need, but the number must evenly divide into 60