1

I got a UITimePicker in my app, and I want to use it to select a certain number of hours and minutes after a user event. (for example 3 hours 15 minutes after 7PM) This is why I want to show the date in 24 hour format, otherwise the user might get confused.

Is there a way to force the timepicker to show 24 hour view, independent of the clock format for the locale? Or do I just pick another locale?

UIDatePicker *timePicker;
NSDateFormatter *timePickerFormatter;

  [self.view addSubview:timePicker];
    float tempHeight = timePicker.frame.size.height;
    timePicker.frame = CGRectMake(0,150 ,self.view.frame.size.width , tempHeight);
    timePicker.hidden = YES;
    timePickerFormatter= [[NSDateFormatter alloc]init ];
    [timePickerFormatter setDateFormat:@"HH:mm"]; //formats date for processing

Thank you!

Update: I found the answer like this:

timepicker.datePickerMode = UIDatePickerModeCountDownTimer;

This effectively accomplishes what I'm trying to do by displaying 0 to 23 hours and 0 to 59 minutes, no further customization required! Sorry for asking about the 24 hour format, this is what the Android app that I'm porting over was using!

Alex Stone
  • 46,408
  • 55
  • 231
  • 407
  • possible duplicate of [UIDatePicker - Problem Localizing](http://stackoverflow.com/questions/2894161/uidatepicker-problem-localizing) – AliSoftware Oct 02 '11 at 16:17
  • 1
    Does this answer your question? [UIDatePicker - Problem Localizing](https://stackoverflow.com/questions/2894161/uidatepicker-problem-localizing) – bad_coder May 07 '22 at 13:13

2 Answers2

1

Did you try to use the calendar and locale properties of UIDatePicker?

That should be the solution for your problem, because as the documentation explains (read the UIDatePicker class reference):

UIDatePickerModeTime

The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].

[EDIT] after further investigation, it seems the NSLocale can't override the AM/PM setting… (I had that information by… simply searching here in Stackoverflow! And your question has already been asked and answered many times before)

Community
  • 1
  • 1
AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • I also tried setting the locale to european locales, like Germany, and it doesn't work. That's why I need to "force"/trick the Date Picker to the 24 hour format, regardless of the location. Would I need to subclass the DatePicker and try to customize it that way? PS. I'll try to change the calendar's timezone/locale first, and see if this would help – Alex Stone Oct 02 '11 at 16:40
  • You didn't mention this in your question so I couldn't guess. Anyway, search StackOverflow there are plenty of related questions and your question have also already been answered multiple times: it seems you can't force the 12/24 hours format of a `UIDatePicker` and have to simply use a customized `UIPickerView` (which is not really complicated to customize anyway) – AliSoftware Oct 02 '11 at 16:54
  • I found that the timepicker can be customized with countdown timer style (used by the timer app). This may work for my app. – Alex Stone Oct 02 '11 at 21:25
0

For visualization I use:

[self.datePickerView setLocale:[NSLocale localeWithLocaleIdentifier:@"ru_RU"]];

You may use your own locale.

And for NSDateFormatter I Use:

[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

It is no longer breaks my applications.

Alexmelyon
  • 1,168
  • 11
  • 18