1

I am making a custom TimePicker from a UIPickerView. I have it working great except when it comes to displaying the hours. I want it to function just like the UIDatePicker so that it follows the users Locale setting. If they have French selected, the time should display in 24 hour format. If they have US it should display in 12 hour format.

I have an array hoursArray that goes from 0 - 23. I then run this code to convert it to the needed format. However it is not wanting to change to 24 hour format if that option is set in the Settings -> International section

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];//[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormatter setDateFormat:@"H"];
NSDate *tempDate = [dateFormatter dateFromString:[hoursArray objectAtIndex:row]];
[dateFormatter setDateFormat:@"h"];

return [dateFormatter stringFromDate:tempDate];

I tried changing [dateFormatter setDateFormat:@"h"]; to [dateFormatter setDateFormat:@"H"]; since Apples doc says the users personal locale setting will override it, but this doesn't seem to be the case.

How do I get the dateFormatter to follow the users locale setting properly?

4

1 に答える 1

0

私の場合、ユーザー設定のオーバーライドはうまく機能しましたが、それは私にとって悪いことでした.!!! 日付フォーマッタのロケールを次のように設定してみてください

[dateFormatter setLocale:[NSLocale systemLocale]];

それでもうまくいかない場合は、NSDateFormatter が自動的にユーザー設定を考慮するため、ロケールを設定したコードを削除してみてください。

于 2013-05-24T06:16:59.217 に答える