6

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?

motionpotion
  • 2,656
  • 6
  • 42
  • 60
  • 1
    `UIDatePicker` Class Reference, try to find the `datePickerMode` property: https://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html, and try to set it to `UIDatePickerModeTime`. there is no such version of the `UIDatePicker` which won't show the minutes, however you can set the `minuteInterval` to show `00` and `30` (whole and half hours) only. – holex Jul 08 '14 at 13:19

2 Answers2

5

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.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
-1

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.

Rion
  • 39
  • 1
  • 2