How to disable AM/PM in UIDatePicker

好久不见. 提交于 2019-11-29 01:19:20

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.

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.

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"]];

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"]];
Freddyo

This removes the AM/PM from the picker...

UIDatePicker *myDatePicker = [[UIDatePicker alloc]init];
[myDatePicker setDatePickerMode:UIDatePickerModeDate];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!