I've got a UIDatePicker
with the time only and I need a possibility to set the minimum and the maximum hour is showed. For example, I need it to start at 6 hours and finish at 15 hrs. Is it possible to perform this with UIDatePicker
?
Asked
Active
Viewed 6,351 times
1

rmaddy
- 314,917
- 42
- 532
- 579

Nikita Shytyk
- 408
- 7
- 16
-
I'm afraid there's no official way to achieve this when using UIDatePicker as a count down timer. However, you could subclass UIPickerView - have a look at this question: https://stackoverflow.com/questions/31023356/how-can-i-set-the-uidatepickermodecountdowntimers-maximum-time/40969597 – neatchuck Jul 07 '18 at 15:57
3 Answers
1
1) Use this function to retrieve the minimum and maximum date
func getTimeIntervalForDate()->(min : Date, max : Date){
let calendar = Calendar.current
var minDateComponent = calendar.dateComponents([.hour], from: Date())
minDateComponent.hour = 09 // Start time
let formatter = DateFormatter()
formatter.dateFormat = "h:mma"
let minDate = calendar.date(from: minDateComponent)
print(" min date : \(formatter.string(from: minDate!))")
var maxDateComponent = calendar.dateComponents([.hour], from: date)
maxDateComponent.hour = 17 //EndTime
let maxDate = calendar.date(from: maxDateComponent)
print(" max date : \(formatter.string(from: maxDate!))")
return (minDate!,maxDate!)
}
2) Assign these dated to your pickerview
func createPickerView(textField : UITextField, pickerView : UIDatePicker){
pickerView.datePickerMode = .time
textField.delegate = self
textField.inputView = pickerView
let dates = getTimeIntervalForDate()
pickerView.minimumDate = dates.min
pickerView.maximumDate = dates.max
pickerView.minuteInterval = 30
pickerView.addTarget(self, action: #selector(self.datePickerValueChanged(_:)), for: UIControlEvents.valueChanged)
}
That's it :)

Preeti Rani
- 685
- 7
- 17
-
this does not work as expected. DatePicker when in Time mode will still let you pick the time past dates.min. – user484691 Oct 10 '17 at 17:46
0
Just use this:
datePicker.maximumDate = myMaxDate;
datePicker. minimumDate = myMinDate;
Where myMaxDate and myMinDate are NSDate
objects

Antonio MG
- 20,382
- 3
- 43
- 62
-
But the hard part is coming up with proper values for `myMaxDate` and `myMinDate` so the time range does what is needed. – rmaddy Nov 08 '13 at 15:52
-
Unfortunately this doesn't apply if the UIDatePicker is set to UIDatePickerModeCountDownTimer (Obj-C) or UIDatePicker.Mode.countDownTimer (Swift). – neatchuck Jul 07 '18 at 15:54
0
As for my problem, the answer with maximumDate
and minimumDate
isn't what I am looking for. Maybe my solution is not that clear and right, but it worked great for me.
I have changed the UIDatePicker
with UIPickerView
, wrote a separate delegate for it:
@interface CHMinutesPickerDelegate () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) NSArray *minutesArray;
@end
@implementation CHMinutesPickerDelegate
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return [self.minutesArray count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.minutesArray objectAtIndex: row];
}
- (NSArray *)minutesArray {
if (!_minutesArray) {
NSMutableArray *temporaryMinutesArray = [NSMutableArray array];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 2; j++) {
[temporaryMinutesArray addObject: [NSString stringWithFormat: @"%i%i", i, j * 5]];
}
}
_minutesArray = temporaryMinutesArray;
}
return _minutesArray;
}
The minutesArray
returns array of: { "05", "10"... etc}. It is an example for custom minutes picker, you can write the same thing for hour or etc, choosing your own maximum and minimum value. This worked nice for me.

Nikita Shytyk
- 408
- 7
- 16