Display Only Hours In UIDatePicker

4.8k Views Asked by At

I have the following code that sets up a UIDatePicker to show only time. I have two of these UIDatePickers displaying and I want to use them to allow the user to select a time range e.g. between 7 am and 5 pm.

UIDatePicker *datePicker=[[UIDatePicker alloc]init];
datePicker.frame=CGRectMake(-20, 0, 180, 200);
datePicker.datePickerMode = UIDatePickerModeTime;
[datePicker setMinuteInterval:0];
[datePicker setTag:1];

How can I set up the UIDatePicker so that it only shows hours and no minutes?

2

There are 2 best solutions below

0
On BEST ANSWER

UIDatePicker isn’t that customizable. You’ll need to set up your own UIPickerView containing the time range you’re looking for. To fit the user’s date/time settings, you should use NSDateComponents and NSDateFormatter to get names for the hours that you need.

0
On

This does not take localization into account and will be wrong in practically any other language.

The best advice would be to file a radar at Apple requesting more customization of this API.