How to disable AM/PM in UIDatePicker

20,794

Solution 1

According to the documentation you should be able to set the locale property of UIDatePicker to a locale that uses a 24 hrs format.

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"];
[datePicker setLocale:locale];
[locale release];

Alas there is a bug causing both simulator and device to continue to layout the date picker according to the users international preferences.

As you suggest, you have to roll your won date picker to be able to display a 24 hrs picker.

Solution 2

AFAIK you can't (within limits of SDK). The AP/PM will disappear if the user chooses to use 24-hour format. Make your own UIPickerView in case this is important.

Solution 3

the AM/PM is linked with the iphone's locale settings. You can either force the locale of the picker to something that doesn't have AM/PM like ro_RO or in case you want it all the time with AM/PM you can go with en_US. For settings this you can do it in designer on the right side under locale like English(United States) or by code

[timePicker setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];

Solution 4

I know this is old... but I was looking into this recently and found this through some testing:

[datePicker setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_FR"]];

This basically tells me that the "language" is English (the "en" part) and "time style" is French (which is default 24hr time) (the "FR" part). I combined the two through trial and error and found that it was working for what I wanted, which was just to have a "Date and Time" picker that also picks in 24 hour time (no AM/PM section on the picker, in English text).

The problem with this is, when you grab the date here, the locale may be wrong ->

- (void)changeDate:(UIDatePicker *)sender {

So do this to fix the locale in the date change selection part:

[sender setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_FR"]];
Share:
20,794
Binay Shrestha
Author by

Binay Shrestha

Updated on September 15, 2020

Comments

  • Binay Shrestha
    Binay Shrestha over 3 years

    How to disable or hide AM/PM in UIDatePicker from code / interface builder?

    I want to have 24 hours time picker mode in UIDatePicker. Please help

  • Binay Shrestha
    Binay Shrestha over 14 years
    I tried en_GB and dk_DK locale does not work in simulator nor in device. But i noticed if u set 24 hrs format in iphone settings then UIDatepicker displays 24 hr time. So should I make my own UIPickerView or is there a way to acheive this?
  • Niels Castle
    Niels Castle over 14 years
    I was wrong in my original answer (now updated) neither device nor simulator works. A bug report has been filed with Apple. For now I'd do as you suggest - roll your own.
  • Vlad Grichina
    Vlad Grichina almost 14 years
    It is not a bug, you are only changing a language, while in reality it depends on country settings.
  • jrturton
    jrturton almost 12 years
    It also removes the time! I don't think this is what's required.
  • Sawan Cool
    Sawan Cool over 11 years
    Thanks Bro its work... datePicker.datePickerMode =UIDatePickerModeTime; NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"]; [datePicker setLocale:locale];
  • DawnSong
    DawnSong almost 9 years
    The problem is that users are not familiar to Danish. So the solution has to be rejected.
  • Niels Castle
    Niels Castle almost 9 years
    It's an example. It lists one locale for brevity
  • Alix
    Alix about 5 years
    I have used this trick. yes, it does display the time in 24 HR format. but the problem starts when you start working with time. when you take the date out of picker you have convert date from en_GB time zone.