Alex Stone
Alex Stone

Reputation: 47364

iPhone iOS 4 UITimePicker force 24 hour view

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!

Upvotes: 1

Views: 4969

Answers (2)

Alexmelyon
Alexmelyon

Reputation: 1249

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.

Upvotes: 0

AliSoftware
AliSoftware

Reputation: 32681

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)

Upvotes: 1

Related Questions